由于解码不当,Wordpress搜索特殊字符失败


Wordpress search failed on special characters due to improper decode

我正在实现Wordpress搜索功能。当我搜索文本"Division的"(这是其中一个帖子中的文本)时,它返回"No results found"

现在进一步调查,我检查了核心文件:wp-includes/query.php => function parse_search()

并发现$term被编码为:Division'xe2'x80'x99s

现在这一项没有被正确解码。最后形成的SQL语句是:
(((test_posts。post_title LIKE '%Division'xe2'x80'x99s%')或(test_posts。post_content LIKE '%Division'xe2'x80'x99s%')))

因此,我想对特殊字符进行解码,以成功地搜索包含特殊字符的术语。

解码方法如下:

  • $string = urldecode($string);
  • $string = html_entity_decode($string);
  • $string = rawurldecode ($string);$string = base64_decode($string);
  • $string = utf8_decode($string);

没有工作。是否有任何插件/钩子/方法可以帮助?

示例:

简单的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字符串不会与百分比等一起填充到文本字段中。

如果我这样做:

$search_query = !empty($search_query) ? trim(sanitize_text_field(urldecode($search_query))) : '';

仍然有一个问题,但不再是文本输入没有正确字符串的问题,问题变成了现在没有搜索结果。

如何解决这个问题与Wordpress搜索?

wp-config.php包含以下内容:

define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');

header.php包含以下内容:

<!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文件中有以下内容:

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编码作为我的字符集。问题是什么,为什么我在URL中搜索:copper(i)/(ii)返回?s=copper%2528i%2529%252F%2528ii%2529 ?我应该找到2个结果,但是我得到0个结果。为什么?

如果我将url更改为:?s=copper(i)/(ii),我看到我的2个结果。但是为什么我不能得到我的结果,和/或url是这样的?我可以诚实地关心什么url结构是少,但我确实希望我的2个结果被发现,当我输入:copper(i)/(ii)进入搜索表单,但目前它没有找到任何结果。

好的,所以你必须解码搜索查询,这是我如何让它工作,现在像一个魅力!现在返回搜索结果,但url保持编码,所以这里没有任何问题。

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中添加以下内容:

RewriteCond %{QUERY_STRING} s=(.*)
RewriteRule ^$ /search/%1? [R,L]

搜索的结构如下:/search/searchterm

使用特殊字符的查询现在也可以工作了。对于CMS的一部分来说,要正常工作是多么痛苦啊。