How to Change Specific Texts and Strings "by Hand" on WordPress
Posted by Nuno Marques on 15 Nov 2018
Introduction
Customizing text strings on WordPress or WooCommerce websites sometimes requires more flexibility than what's offered through traditional translation methods. In this tutorial, we'll explore a method that allows you to change specific texts and strings manually, without directly modifying core files.
Tutorial
The change_texts_by_hand
Function:
The function provided below offers a straightforward way to change texts, titles, or strings on WordPress and WooCommerce pages. By using a switch
statement, you can define specific cases where text replacements should occur.
function change_texts_by_hand( $translated_text, $text, $domain ) {
switch ($translated_text) {
case 'Additional Information':
$translated_text = __('Details afhaling', 'woocommerce');
break;
case 'Order Tracking':
$translated_text = __('Uw bestelling', 'woocommerce');
break;
}
return $translated_text;
}
add_filter( 'gettext', 'change_texts_by_hand', 20, 3 );
When to Use It
This function is particularly useful when you're translating a website and encounter text that isn't automatically translated. Instead of directly editing files, which can be problematic during updates, this method offers a safer alternative.
Please Note
While this approach provides a convenient solution for changing texts, it's essential to use it judiciously. Reserve it for cases where traditional translation methods are insufficient. For multilingual websites, consider updating .po
files in /wp-content/languages/
instead.