Concatenating esc_attr_e() to a variable issue in WordPress

Viewed 122

i am trying to write wordPress search plugin. Everything is working fine but when i put all code inside my shortcode function then esc_attr_e() is not functioning inside php. i don't know how it works for me. here is my line:

<button type="submit" title="<?php esc_attr_e('Search', 'woocommerce'); ?>" class="search"><span><?php esc_attr_e('Search','woocommerce');?></span></button>

and here is how i am trying to achieve this inside php:

<?php
$search = '<button type="submit" title="'.esc_attr_e('Search', 'woocommerce'). '" class="search"><span>';
$search .= ''.esc_attr_e('Search','woocommerce').'</span></button></form>';
?> 

Any help is highly appreciated

1 Answers

The function esc_attr_e() echoes (display) translated text that has been escaped, so that's why you get an issue as you are at the same time trying to concatenate it in a variable.

Instead use simply esc_attr__()function that can be concatenated without issues as it returns a translated text that has been escaped.

<?php
$search = '<button type="submit" title="'.esc_attr_e('Search', 'woocommerce'). '" class="search"><span>';
$search .= ''.esc_attr__('Search','woocommerce').'</span></button></form>';
?> 
Related