由于解码不正确,Wordpress搜索特殊字符失败 [英] Wordpress search failed on special characters due to improper decode
问题描述
我正在实现Wordpress搜索功能。
当我搜索文本 部门的(其中一个帖子中的文本)时,
它返回未找到结果
I am implementing the Wordpress search functionality. When I search for text "Division's" (which is a text in one of the posts), It returns "No results found"
现在要进一步调查,我检查了核心文件:
wp-includes / query.php => function parse_search()
Now to investigate further, I checked the core file: wp-includes/query.php => function parse_search()
,发现收到的$ term编码为: Division\xe2\x80\x99s
And found that the $term is received encoded as : Division\xe2\x80\x99s
现在,此术语未正确解码。最终形成的SQL语句为:
(((((test_posts.post_title LIKE'%Division\xe2\x80\x99s%'))OR((test_posts.post_content LIKE'%Division\xe2\ \x80\x99s%')))
Now this term is not decoded properly. And the final SQL statement formed is :
(((test_posts.post_title LIKE '%Division\xe2\x80\x99s%') OR (test_posts.post_content LIKE '%Division\xe2\x80\x99s%')))
因此,我想对特殊字符进行解码,以便也可以成功搜索具有特殊字符的项。
So, I want to decode the special characters to succesfully search terms with special characters too.
解码方法,例如:
- $ string = urldecode($ string);
- $ string = html_entity_decode($ string);
- $ string = rawurldecode($ string);
- $ string = base64_decode($字符串);
- $ string = utf8_decode($ string);
- $string = urldecode($string);
- $string = html_entity_decode($string);
- $string = rawurldecode ($string);
- $string = base64_decode($string);
- $string = utf8_decode($string);
不起作用。
是否有任何可以帮助您的插件/挂钩/方法?
Did not work. Is there any plugin/hook/method that can help?
提供的示例:
Example Provided:
简单的 searchform.php
文件在这里:
if (!defined('ABSPATH')) exit(0);
global $wp_query;
$search_query = get_search_query();
$error = get_query_var('error'); ?>
<form role="search" method="get" class="search-form form-inline" action="<?php echo esc_url(home_url('/')); ?>">
<input id="mod-search-searchword" type="search" size="30" class="inputbox search-query search-field" placeholder="search products, content" value="<?php echo !empty($search_query) && empty($error) ? $search_query : ''; ?>" name="s" title="Search for:" />
<input type="submit" class="button btn btn-primary" value="Search" />
</form>
现在,如果我输入()
他们得到了urlencoded,并且相同的urlencoded字符串没有填充百分比等的文本字段。
Now if I type in characters like ()
they get urlencoded, and that same urlencoded string not populates into the text field with the percentages, etc.
如果我这样做:
$search_query = !empty($search_query) ? trim(sanitize_text_field(urldecode($search_query))) : '';
仍然存在问题,但不再是文本输入不具有正确字符串的问题,问题变成现在没有搜索结果。
There is still a problem, but no longer a problem with the text input not having correct string, the problem becomes that there are no search results now.
如何使用Wordpress搜索解决此问题?
How to fix this issue with Wordpress Search?
wp -config.php包含以下内容:
wp-config.php contains the following:
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');
header.php包含以下内容:
header.php contains the following:
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=3.0, user-scalable=yes"/>
<meta name="HandheldFriendly" content="true" />
<meta name="apple-mobile-web-app-capable" content="YES" />
<link rel="shortcut icon" href="<?php echo get_stylesheet_directory_uri(); ?>/favicon.ico" type="image/vnd.microsoft.icon" />
<title><?php wp_title(' - ', true, 'right'); ?></title>
<?php wp_head(); ?>
</head>
我在我的functions.php文件中具有以下内容:
I have the following in my functions.php file:
function livchem_searchfilter($query) {
global $search_query;
if ($query->is_search && !is_admin()) {
// check if length of query > 3 but < 200
$search_query = trim(get_search_query());
$search_length = strlen($search_query);
if ($search_length < 3 || $search_length > 200)
{
$query->set('error', 'Search term must be a minimum of 3 characters and a maximum of 200 characters.');
return $query;
}
else
{
$query->set('post_type', array('post', 'page', 'product'));
$query->set('posts_per_page', 20);
}
}
return $query;
}
add_filter('pre_get_posts','livchem_searchfilter');
因此,我确实将UTF-8编码作为我的字符集afaik。什么是问题,为什么要搜索: copper(i)/(ii)
返回?s = copper%2528i%2529%252F% URL中的2528ii%2529
?我应该为此找到2个结果,但发现0个结果。为什么?
So, I do have UTF-8 encoding as my charset afaik. What is the problem, why is my search for: copper(i)/(ii)
returning ?s=copper%2528i%2529%252F%2528ii%2529
in the URL? And I should have 2 results found for this, but I get 0 results found. Why?
如果我将网址更改为:?s = copper(i)/(ii)
我看到了2个结果。但是为什么我不能得到我的结果,和/或这样的网址?老实说,我不太在乎url结构是什么,但是我确实希望在输入以下内容时找到2个结果: copper(i)/(ii)
到搜索表单,但目前找不到任何结果。
And if I change the url to this: ?s=copper(i)/(ii)
I see my 2 results. But why can't I get my results, and/or the url to be like this? I could honestly care less on what the url structure is, but I do want my 2 results to be found when I type in: copper(i)/(ii)
into the search form, but currently it is not finding any results.
推荐答案
好,所以您必须对搜索查询进行解码,这里是我如何运作,现在像魅力一样运作!现在,这将返回搜索结果,但会保留url的编码,因此在此处没有任何问题。
Ok, so you do have to decode the search query and here's how I have it working and works like a charm now! This now returns the search results, but keeps the url encoded so no problems anywhere here.
function livchem_search_filter($s) {
return urldecode($s);
}
add_filter('get_search_query', 'livchem_search_filter');
add_filter('the_search_query', 'livchem_search_filter');
function livchem_query_vars_search_filter($query)
{
if ($query->is_search && !is_admin()) {
$query->query_vars['s'] = urldecode($query->query_vars['s']);
}
return $query;
}
add_action('parse_query', 'livchem_query_vars_search_filter');
另外,这对于与路径相关的搜索现在也很有效,因此,如果我将以下内容添加到我的.htaccess:
As a plus this also works well for path related searches now, so if I added the following to my .htaccess:
RewriteCond %{QUERY_STRING} s=(.*)
RewriteRule ^$ /search/%1? [R,L]
搜索的结构如下: / search / searchterm
带有特殊字符的查询现在也可以使用。对于CMS的一部分来说,正常工作真是让人头疼。
And the query with special characters also now works. what a pain in the neck this was to get working properly, for something that is part of the CMS.
这篇关于由于解码不正确,Wordpress搜索特殊字符失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!