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.
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:
Note that I’m using
get_search_query()
, because it simply returns value instead echoing it, also it passes query sting throughesc_attr()
and no need foresc_html()
.The key is to use
%s
placeholders in combination with thesprintf
orprintf
function.http://php.net/sprintf
Also note that
the_search_query()
outputs its result directly, you needget_search_query()
which returns the result.