Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to create a function for the title inside my functions.php, I'm having a problem with the search title part where I have to concatenate here's the part with issues:

elseif (is_search()) {
_e('Search for ', 'textdomain') . '"' . esc_html(the_search_quey()) . '$quot; - '; }

this one gives a title like this: Search for searchtermsitename

no quotes and the search term is attached to the site name.

I tried this one:

elseif (is_search()) {
_e('Search for ' . '"' . esc_html(the_search_quey()) . '$quot; - ', 'textdomain'); }

it doesn't work either, this one outputs: searchtermSearch for "" sitename

it's better than the first one but he search query is before, and can't figure out why.

so any help, and thanks in advance.

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

the_search_query() echoes itself, so by putting it into another echo function (what _e() is) you'll get result as in second example.

It isn't recommended to use variables or function inside l18n functions, because they can't be translated, for more information see Otto's: Internationalization: You’re probably doing it wrong.

So you should use code like this:

printf( __( 'Seach for "%s", 'textdomain' ), get_search_query() );

Note that I'm using get_search_query(), because it simply returns value instead echoing it, also it passes query sting through esc_attr() and no need for esc_html().

share|improve this answer
    
thank you very much for the info –  Peter Mar 27 '12 at 9:00
    
Great article by Otto you linked to. Thanks. –  Geert Mar 27 '12 at 9:02
    
You're very welcome. –  Mamaduka Mar 27 '12 at 9:09
    
I like it when I go to answer a question and somebody's already quoted me. :) –  Otto Mar 28 '12 at 1:40
    
@Otto quoting you will be trending this year, WordCamp San Diego showed it. :) –  Mamaduka Mar 28 '12 at 8:25
add comment

The key is to use %s placeholders in combination with the sprintf or printf function.

printf(__('Search for “%s”', 'textdomain'), esc_html(get_search_query());

http://php.net/sprintf

Also note that the_search_query() outputs its result directly, you need get_search_query() which returns the result.

share|improve this answer
    
it's still giving the same output like my second try –  Peter Mar 27 '12 at 8:52
    
Updated with get_search_query() instead of the_search_query(). –  Geert Mar 27 '12 at 8:57
    
thank you for your help –  Peter Mar 27 '12 at 9:01
add comment

Your Answer

 
discard

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.