Would you like to change any plugin’s text(string) without modifying plugin files? For example, if you like to change WooCommerce to E-Commerce. We can use the below filters of WordPress to change the text.
"gettext" and gettext_{domain_name}
Filter gettext
We can use this filter whenever we don’t know which text domain is used for the text we want to change. For example, we want to shift WooCommerce to E-Commerce on the admin left menu of WordPress. However, we do not know which text domain is used to print that text. We can do as like the example below.
add_filter( 'gettext', 'twc_change_text_without_text_domain', 0, 3);
/**
* Filter Text.
*
* Filter text by existing text
*
* @param string $translated_text The translated version of the string.
* @param string $untranslated_text The untranslated version of the string.
* @param string $domain The text domain of the i18n function.
*/
function twc_change_text_without_text_domain( $translated_text, $untranslated_text, $domain ) {
if ( 'WooCommerce' === $untranslated_text) {
return 'E-Commerce';
}
return $translated_text;
}
Filter gettext_{domain_name}
You can use this filter when you know the text is printed with the specific text domain. I always preferred to use this filter when you know the text domain name. Because if you add many more unwanted filters then put load on site. With the text, domain filter was only called when the string was for the specific domain.
add_filter( 'gettext_woocommerce', 'twc_change_text_with_text_domain', 0, 3);
/**
* Filter Text.
*
* Filter text by existing text
*
* @param string $translated_text The translated version of the string.
* @param string $untranslated_text The untranslated version of the string.
* @param string $domain The text domain of the i18n function.
*/
function twc_change_text_with_text_domain( $translated_text, $untranslated_text, $domain ) {
if ( 'WooCommerce' === $untranslated_text) {
return 'E-Commerce';
}
return $translated_text;
}
If it still doesn’t work, please reach out to us. We are happy to help you.