I just made a built-in translation function to help users to translate their theme from theme options.

Usually my functions works great as follow:

<?php _e( 'No Comments', 'mytextdomain' ); ?>


<?php echo __myfunction( 'no_comments' ); ?>

My question is related to esc_attr__ and I'm stuck here... How the following code should look using custom translation function?

esc_attr__( 'No Comments', 'mytextdomain' )

Where should I place my translation function __myfunction ? Will be corect the following code?

esc_attr__myfunction( 'no_comments' )


  • 1
    I'm sorry but I really don't understand your question. If you have made your own tranlation function you could choose whatever name you want. I don't get where the problem is. I think you could use esc_attr__( __myfunction( 'no_comments' ) ) or esc_attr_e( __myfunction( 'no_comments' ) ) for echoing the result.
    – cybmeta
    Commented Dec 27, 2014 at 9:56
  • Yes, you understood perfectly!Please post your comment as a reply so that I can accept it. Thanks.
    – Knott
    Commented Dec 27, 2014 at 10:01

2 Answers 2


As your function returns the translated string, you could pass the translation function as parameter for esc_attr() and esc_attr_e():

esc_attr__( __myfunction( 'no_comments' ) );
esc_attr_e( __myfunction( 'no_comments' ) );

But esc_attr__() and esc_attr_e() will perform translation tasks that you don't need because you handle the translation at your own, so I think it is better to use esc_attr() only:

esc_attr( __myfunction( 'no_comments' ) );


echo esc_attr( __myfunction( 'no_comments' ) );

You can use esc_attr_e():

esc_attr_e( $text, $domain )

Displays translated text that has been escaped for safe use in an attribute. Encodes < > & " ' (less than, greater than, ampersand, double quote, single quote). Will never double encode entities.

  • I guess you missed my question: I need to use my custom translation function. Where I can place __myfunction using esc_attr__?
    – Knott
    Commented Dec 27, 2014 at 9:23
  • Please add context of your question, because gettext is for string, not for functions. And why do you need such a function there? And why not the esc_attr_e() function is doing your job. Please edit your question and add more context and logic. Commented Dec 27, 2014 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.