4

I'd like to translate one error message from a plugin that blocks spam from our forms but occasionally blocks legitimate (live-person) form submissions. The message is coded to be translateable - in the plugin itself, the line is:

$err_msg = __( 'That action is currently not allowed.' );

My problem is, my site is already in U.S. English just like the plugin, and technically when I "translate" this string so that visitors see a "it's not your fault; please contact so-and-so so we can fix this issue for you" message, it will still be in U.S. English. I've set up a textdomain in my theme and was able to generate the .pot, .po and .mo files to translate this one string. The problem is, since I'm setting wp-config to en_US, it's not actually changing anything.

Since I don't have an exact process to trigger the error on the forms themselves, I set up an admin dashboard widget to test the translation. Sure enough, if the contents of the widget are

$err_msg = __( 'That action is currently not allowed.' );
echo $err_msg;

I get 'That action is currently not allowed.' If I change the widget contents to

$err_msg = __( 'That action is currently not allowed.', 'mytextdomain' );
echo $err_msg;

I get the custom message that I'm trying to achieve.

I don't want to edit the plugin's files themselves since they would revert every time there's an update. There's no filter or hook in the plugin for this particular message - there are filters for other errors, just not this generic one. Is there a better approach to getting this one string of user-facing text changed? I want something server-side rather than a JS/jQuery solution, if at all possible.

5

The gettext filter can be used to change strings that use the gettext functions.

add_filter( 'gettext', 'wpse_change_error_string', 10, 3 );
/**
 * Translate text.
 *
 * @param string $translation  Translated text.
 * @param string $text         Text to translate.
 * @param string $domain       Text domain. Unique identifier for retrieving translated strings.
 *
 * @return string
 */
function wpse_change_error_string( $translation, $text, $domain ) {
    // The 'default' text domain is reserved for the WP core. If no text domain
    // is passed to a gettext function, the 'default' domain will be used.
    if ( 'default' === $domain && 'That action is currently not allowed.' === $text ) {
        $translation = "This is the modified version of the original string...";
    }

    return $translation;
}

Note: Fortunately, the WordPress core does not currently use the string That action is currently not allowed., so that message will not be undesirably changed using the code above. But, that would happen if the core did use that message. This is one reason why it's important for plugins and themes to use their own unique text domain.

The gettext family of functions (__(), _e, etc) should always be passed a text domain though. The exception to this rule is for the WordPress core, which doesn't pass a text domain explicitly, but it still has one named default.

I would contact the plugin's authors and suggest that they add their own text domain to their gettext function calls.

  • 2
    Fantastic - thanks, Dave, that worked perfectly. Good point about this affecting any instance of that wording; in my case if that does happen, it will be all right because I will still want whoever reads the message to contact me so I can fix whatever is wrong. I will follow up with the plugin author to see if they'll add a plugin textdomain. – WebElaine May 17 '17 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.