WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When using the_search_query to display search result on my site, the spaces between words are displayed with a + symbol.

So if a visitor searches for "wordpress plugins" the_search_query output in search.php returns


How can I remove the + symbol, and/or replace it with non-break space entity, like so .....

    wordpress plugins
    wordpress plugins

I tried this approach but it is not working .....

        $string = the_search_query();
        $res = preg_replace("/[^a-zA-Z]/", "", $string);
        echo $res;
share|improve this question
The plus sign is automatically decoded to a space in the search query. If that doesn't happen in your installation you have a bug in your theme or in a plugin. – toscho Apr 17 '12 at 0:49
It must be the case in this instance, because neither of @Joshua's code samples fixed the problem (which you would expect them to do in normal circumstances). Thanks for your help. – TheLoneCuber Apr 17 '12 at 2:11
up vote 0 down vote accepted

Personally, when I do things like this I use str_replace() [Link]

Using your above example it would be implemented like so:

    $string = the_search_query();
    $res = str_replace("+", " ", $string);
    echo $res;

That will replace any + with a space.

Or if you want the &nbsp; use this:

    $string = the_search_query();
    $res = str_replace("+", "&nbsp;", $string);
    echo $res;

On a minor note, this question is not really WP related and may get bounced to StackOverflow or closed by a mod.

share|improve this answer
Thanks Joshua. I think @toscho might be right with his "theme/plugin bug" because neither of your code samples fix the problem. Thanks for your help. – TheLoneCuber Apr 17 '12 at 2:12
In which case uploading a clean version of the WP core files should fix the problem. – Joshua Apr 17 '12 at 2:24

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.