I am validating plugin to make it compatible with WPCS. a function which contains gettext placeholder returning error when i run phpcs command. Source code and screenshot of error is attached for reference. Maybe i am missing something or doing it in wrong way?

 * Add Error.
 * @package Mypackage
 * @since 0.1.0
 * @param string $code Error Code.
 * @param string $message Error Message.
 * @return object WP_Error Returns Error Object.
function mypackage_add_error( $code, $message ) {

    /* translators: %s: Error Message */
    return new WP_Error( $code, sprintf( esc_html__( '%s', 'mypackage' ), $message ) );

Screenshot of Error. enter image description here

  • there is nothing to translate on this ligne then the code is the same as doing return new WP_Error( $code, $message);
    – mmm
    Commented Feb 24, 2018 at 21:38
  • @mmm Thanks but its supposed to translate error messages. does that make sense or need to wrap message itslef in gettext wrapper while calling mypackage_add_error?
    – Anwer AR
    Commented Feb 24, 2018 at 21:42
  • mypackage_add_error seems somewhat redundant, would it not be easier to just create the WP_Error object in place rather than calling mypackage_add_error? mypackage_add_error is not the place for translation, and %s is not something to be translated
    – Tom J Nowell
    Commented Feb 24, 2018 at 21:44
  • @TomJNowell actually i was feeling awkward to create instances of WP_Error every-time which i am still doing in mypackage_add_error. not sure if its good or bad practice but eventually it was supposed to find a nicer way. so instead of calling WP_Error directly i wrap it in a function to easily modify in future. so my point of interest is maybe: can we use dynamic strings from variables with translation functions? just like i do with $message in above function
    – Anwer AR
    Commented Feb 24, 2018 at 21:55
  • 2
    Translation functions can't use variables because php is not executed to extract translatable strings.
    – Milo
    Commented Feb 24, 2018 at 22:16

2 Answers 2


There are several things wrong with your code

  1. The is no need for translation there. The message should have been translated before it is being passed to that function. At that function there is actually no context at all to create any different translation than %s

  2. Escaping should happen at the output. You should escape the result of the sprintf and the translation.

And keep in mind that WPCS is a tool to help you, not the bible. At least at its current stage it lacks too many feature to just blindly follow it all the time. Some of the errors it emits right now are due to bad parsing of modern PHP or lacking features, so if you are 100% sure that in your specific case %s should be translatable go with it.


You're trying to translate a placeholder %s which is not a translatable content and it'll be replaced with a variable later. It's best to pass the translated $message instead of a translatable $message.

You should write the function definition this way

function mypackage_add_error( $code, $message ) {
    return new WP_Error( $code, $message );

And the function mypackage_add_error() should be used this way

mypackage_add_error( 'test-error', esc_html__( 'Test error message', 'text-domain' ) );
  • Hi @objPlabon, Thank you for your explanation I got to learn and focus on Translatable and Translated content. great explanation
    – Anwer AR
    Commented Feb 25, 2018 at 23:25
  • Great, stay focused and keep learning :)
    – obiPlabon
    Commented Feb 26, 2018 at 6:40

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.