0

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' ); ?>

becomes:

<?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' )

Thanks

  • 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 Dec 27 '14 at 9:56
  • Yes, you understood perfectly!Please post your comment as a reply so that I can accept it. Thanks. – Knott Dec 27 '14 at 10:01
1

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

and

echo esc_attr( __myfunction( 'no_comments' ) );
0

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 Dec 27 '14 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. – Mayeenul Islam Dec 27 '14 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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