Pergunta

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?

Foi útil?

Solução

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' ); ?>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a wordpress.stackexchange
scroll top