Using Gettext and NGettext to Replace Arrays of Words in the WordPress Admin Panel

Home » Snippets » Using Gettext and NGettext to Replace Arrays of Words in the WordPress Admin Panel
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Have you ever needed to replace a specific set of words or phrases in the WordPress admin area? Maybe you need to change terminology to match your industry or company-specific language. The good news is that WordPress has built-in functions called gettext and ngettext that allow you to easily replace text within the admin area. In this article, we’ll show you how to utilize these functions to make those changes quickly and efficiently.

					function wpturbo_replace_admin_words( $translated_text, $text, $domain ) {
    $replace_array = array(
        'word1' => __( 'Replacement1', 'wpturbo' ),
        'word2' => __( 'Replacement2', 'wpturbo' ),
        'word3' => __( 'Replacement3', 'wpturbo' ),
    );
    $translated_text = str_replace( array_keys( $replace_array ), $replace_array, $translated_text );
    return $translated_text;
}
add_filter( 'gettext', 'wpturbo_replace_admin_words', 20, 3 );
add_filter( 'ngettext', 'wpturbo_replace_admin_words', 20, 3 );
				

This code snippet is a useful tool for replacing certain words or phrases with another set of words or phrases throughout the WordPress admin dashboard. This can be particularly helpful if you want to customize the terminology used in the dashboard for your plugin or theme.

The function wpturbo_replace_admin_words() takes three arguments: $translated_text, $text, and $domain. $translated_text is the translated text that needs to be replaced, $text is the original untranslated text, and $domain is the text domain.

We define an array, $replace_array, where the keys represent the words to be replaced, and the values are the replacement words. In this example, we have three key-value pairs. If we have additional words we want to replace, we can simply add them to the array.

$replace_array = array(
    'word1' => __( 'Replacement1', 'wpturbo' ),
    'word2' => __( 'Replacement2', 'wpturbo' ),
    'word3' => __( 'Replacement3', 'wpturbo' ),
);

Next, we use the str_replace() function to replace the words with their corresponding replacements. We achieve this by using array_keys() to extract the keys from the $replace_array, and then pass the $replace_array and the keys as arguments to str_replace(). This way, all occurrences of the words in the $replace_array with their replacements will be replaced.

$translated_text = str_replace( array_keys( $replace_array ), $replace_array, $translated_text );

Finally, we add two filters, gettext and ngettext, and pass the function wpturbo_replace_admin_words() as the callback function to the filters. This ensures that the function is run whenever the WordPress translation functions, __() and _n(), are called within the WordPress admin dashboard.

add_filter( 'gettext', 'wpturbo_replace_admin_words', 20, 3 );
add_filter( 'ngettext', 'wpturbo_replace_admin_words', 20, 3 );

Now, each time we call __() or _n() with one of the words in the $replace_array as the argument, it will be replaced with its corresponding replacement.

Register an account to save your snippets or go Pro to get more features.