I want to translate constant strings in my plugin defined as:
define( 'CONSTANT', __( 'string-A', 'textdomain' ) );
And later using it somewhere like:
$x= '<h4>'.CONSTANT_NAME.'<h4>'; echo $x;
But I am still getting English text even after adding that locale's .mo file & the text-domain. This is happening only for strings defined by PHP constants, rest all strings are working fine. So, does strings defined using PHP constants not work for translation as even in popular translated plugins I can't find any constants containing a string that has been translated?

  • when / where do you try to do this? the text domain isn't loaded yet when plugin files are loaded.
    – Milo
    Commented Aug 19, 2017 at 23:03
  • I am loading text-domain via load_plugin_textdomain() function attached to plugins_loaded hook. I am defining strings as constants in a xyz_strings.php file and later using the string as mentioned above($x= '<h4>'.CONSTANT_NAME.'<h4>'; echo $x;) in a separate php file Commented Aug 20, 2017 at 0:41

1 Answer 1


I think this is happening because Constants cannot be redefined later. Once they are set, they are fixed. http://php.net/manual/en/language.constants.php

I'm not exactly sure how WP language constructs work, but part of me thinks that they are defined, then changed later on the fly when plugins/themes use them. I don't have a reference for this but it seems likely based on what you are experiencing.

i.e. Constant is set with English text the when the code has initialized, but later, when a different language is set, the language functions hot swap the text...except that with constants, since they cannot be redefined, they stay in English.

Hopefully someone else can confirm/deny this behavior.

For what it's worth, Constants are a bad place to store dynamic information (like text to be output). They are better used for server location references that don't change during the execution of a script.

If you want to share variables between very different sections of your site, then perhaps consider making a reference in the $GLOBALS array. http://php.net/manual/en/reserved.variables.globals.php or create a simple function that just returns the information you want it to return after being filtered for language.


I ran a test on the only multi-language site I have. I temporarily tweaked the translate function to make it echo my output before returning it (but after filtering), and I got a list of English strings before the Spanish translation kicked in.

My suspicion is that early on the strings are processed so that translation functions can see what strings are available for translating. Since constants cannot be changed after they are defined, they cannot be translated after they are defined in English.

  • Ok, so are constants initialized even before the text-domain is loaded via load_plugin_textdomain() function attached to plugins_loaded hook. Commented Aug 20, 2017 at 0:38
  • Possibly. I haven't dug too deep in the WP code for language translating to figure this out firsthand, but the strings are probably defined then redefined when a different language is chosen (which would make English the fallback language if no other language pack or translation is present). Commented Aug 20, 2017 at 1:40
  • 1
    Updated response to include a test that I ran. All the strings process in English first, then again with the translated filters. Out of curiosity, why are you wanting to use constants to store translatable strings? Commented Aug 20, 2017 at 13:21
  • I was planning to use constants so that I can put all my strings in a separate file named xyz_strings.php for easy management. But now I am thinking to use a static array in that class containing all strings. Commented Aug 20, 2017 at 15:46
  • Yeah, that sounds like a better plan. I've often used something like this with a function that returns an associated array of values that I want to be able to reuse in many parts of my site. If my response helped you out, I'd appreciate marking it as an answer. Happy coding. Commented Aug 20, 2017 at 16:16

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.