Translate a Constant while appeasing WordPress PHPCS

The following works but isn't up to snuff with PHP Code Sniffer WordPress coding standards

?php esc_html_e( ADDRESS, 'wprig' ); ?

Linter yells at me with:

[WordPress.WP.I18n.NonSingularStringLiteralText] The $text arg must be a single string literal, not "ADDRESS".

The following, for aforementioned error, also don't work:

?php esc_html_e( (string)ADDRESS, 'wprig' ); ?
?php esc_html_e( strval(ADDRESS), 'wprig' ); ?
?php esc_attr_e( ADDRESS, 'wprig' ); ?

I know constants can be exploited so it is needed. Any way to make this work besides //phpcs:ignore, or is this not good practice and I should redo my use of constants?

Topic coding-standards escaping Wordpress

Category Web


I was having a hard time finding the proper solution if it wasn't a constant.

This works for a variable and the value attribute:

<input type="hidden" value="<?php echo esc_attr( $image_id ); ?>">

You cannot use constants or anything other than actual strings with translation functions.

This is because the code that reads your code, and produces the translatable strings does not actually run your code, it is reading your code.

Here is a more detailed post on the topic:

http://ottopress.com/2012/internationalization-youre-probably-doing-it-wrong/

But the short version is this:

This is wrong:

<?php esc_html_e( ADDRESS, 'wprig' ); ?>

Nothing will make that right except this:

<?php esc_html_e( 'Actual String here', 'wprig' ); ?>

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.