0

Inside of a plugin, I'm using the gettext filter to alter strings of a theme so that I don't have to copy the template files over to a child theme and edit them. Also, this isn't always possible since many of the strings are inside class definitions in the theme setup files. Here is my code:

function ctf_filter_gettext($translated, $original, $domain) {

    // This is an array of original strings
    // and what they should be replaced with
    $strings = array(
        'Howdy, %1$s' => 'Greetings, %1$s!', //wordpress core
        'Apply for Job' => 'Reply by Email', //theme specific
            // Add some more strings here
    );

    // See if the current string is in the $strings array
    // If so, replace it's translation
    if (isset($strings[$original])) {
        // This accomplishes the same thing as __()
        // but without running it through the filter again
        $translations = &get_translations_for_domain($domain);
        $translated = $translations->translate($strings[$original]);
    }

    return $translated;
}

add_filter('gettext', 'ctf_filter_gettext', 99, 3);

Inside of the theme there is this line, for example:

__('Apply for Job','jobify')

You can see in the above code that I want to change that to 'Reply by Email'

But this doesn't work.

The other line in the code (Howdy...) is part of Wordpress core, and that DOES work. In fact, I can override any other plugin's translatable text or anything from Wordpress core with the above code.

What would cause this particular theme to ignore my string translations? Note, it doesn't matter if the priority of the filter is 1, 99, or 9999, it still doesn't work.

0

Filtering gettext is horrible for performance. I would strongly recommend to try do it via any other approach. And if all else fails to add and remove filter with extreme precision around necessary parts.

As for why it fails my educated guess would be — timing. It is quite common for strings to be centralized and processed early in the process. For example WP's own labels for post types and taxonomies are processed like that.

It might be that by the time you have added your filter, the theme had already generated string a stashed away a final copy somewhere to make use of.

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.