How can I enable the translation for a text that is not printed on the screen? I was recently implementing the translation to my plugin and everything worked as expected. But doing the tests I realized that I was missing something. There is a small piece of code in my plugin that creates terms based on a condition, but one of them is a text and I need it to be translated. how can I do? I understand that __ and _e are just for static strings that are printed to screen.

if ( empty ( $cookie ) ) {
    wp_set_post_terms( $post_id, '- Default', 'directory' );
else {
    wp_set_post_terms( $post_id, $cookie, 'directory' );

(- Default) is what I need to translate

Thanks to whoever can help me

  • can you be more specific about translation? Do you mean the localisation API built into WordPress for static strings? Or do you mean a plugin such as polylang or WPML for translating content from the database? __ and _e are not meant to be used with dynamic values
    – Tom J Nowell
    Apr 14, 2022 at 12:37
  • I mean the wordpress API, the .mo and .po files. That's what I have implemented.
    – choseɳ
    Apr 14, 2022 at 12:46
  • ok, the mo/po system is not intended for terms and other dynamic content, it's for static strings in PHP files, what you have in your question is dynamic data, nothing in your code block is appropriate for that system
    – Tom J Nowell
    Apr 14, 2022 at 13:00

1 Answer 1


There is a small piece of code in my plugin that creates terms based on a condition, but one of them is a text and I need it to be translated. how can I do? I understand that __ and _e are for the text that is printed on the screen.

You don't, this is not something WordPress supports out of the box and you've misused the functions, you should not be using __ or _e for dynamic content that came from the database or the user.

__ and _e are a part of the localisation API, intended for static strings in your theme and plugins such as labels.

For example:

<p><?php echo __( 'Name', 'mytextdomain' ); ?>: <?php echo esc_html( $name ); ?></p>

In the above example, trying to pass $name, or a posts title/content into the __ function would be extreme bad practice and a major mistake. Many would consider it a bug.

To translate content from the database such as tags, terms, posts, etc, you need to install a plugin.

As for where it works, __ should work everywhere regardless of context.

  • Maybe I'm not explaining myself correctly. But I am not doing what you say, I am using __ and _e only for static strings. I understand from the beginning that they are not for anything else. Now I only need to translate that word (- Defautl) and I don't know how to do it, that's why I asked the question.
    – choseɳ
    Apr 14, 2022 at 13:00
  • you don't, - Default is a reference to something in the database, it would be inappropriate to translate it using __, the same goes for any other function parameter
    – Tom J Nowell
    Apr 14, 2022 at 13:01
  • No no, it does not refer to anything, it is just a written text, the logic would be, if the string is empty, create the term with name (- Defautl). But it is just a text written by me, it does not refer to anything.
    – choseɳ
    Apr 14, 2022 at 13:08
  • - Default would change depending on the users locale, resulting in that parameter changing so different terms would get used, it's dynamic data. Here - Default is not referring to output, it's referring to the name of something in the database, that's not what __ is for. This API is intended for localising static strings on output, not translating strings on input
    – Tom J Nowell
    Apr 14, 2022 at 13:10
  • For example, if the user chose Spanish then it would localise to - Defecto, so that user would run this code and Defecto not Default would be passed to wp_set_post_terms and you would get a brand new term named - Defecto in the database, it would not set - Default as the term. If you want to translate Default for localisation you need a plugin to translate terms, even if it's a term you created
    – Tom J Nowell
    Apr 14, 2022 at 13:14

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.