Wordpress search failed on special characters due to improper decode

≯℡__Kan透↙ 提交于 2019-12-02 22:15:11

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');

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]

Searches would be structured like so: /search/searchterm

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!