John Davidson

php - Concatenating esc_attr_e() to a variable issue in WordPress

0 comments
Message:


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



My Answer:
The issue you are facing is because esc_attr_e() function is used to echo the translated and escaped string, not to return it. So when you try to concatenate it to a variable, it will not work as expected.

To solve this issue, you can use esc_attr() function instead of esc_attr_e() inside your PHP code. esc_attr() function will return the escaped string instead of echoing it.

Here is the updated code:

php
$search = '';
?>


By using esc_attr__() function, you will be able to concatenate the escaped string to your variable without any issues.

Rate this post

3 of 5 based on 7784 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us