How to concatenate inside the _e() function the right way?

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.

Solutions Collecting From Web of "How to concatenate inside the _e() function the right way?"

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().

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

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