WordPress: How to change string translations without a plugin

WordPress: How to change string translations without a plugin

WordPress: How to change string translations without a plugin - Photo / screenshot: T.Bortels/cpu20.com

Often a WordPress installation comes with a variety of different languages you can select from. And with every language you get a language file, that is basically containing the various translated strings. Most of those string translations are rather ok – but you may still want to change some of those string translations. You could of course install a plugin for that – basically WMPL or Loco Translate would let you alter those translated strings – but that may often be just a bit too much, if you just want to adjust a single string. So – here is a quite guide on how to change string translation without a plugin.

In my case I wanted to change the translation of some strings in a WooCommerce powered website: we needed to have “objects” instead of “products” because that shop is basically not selling products – but objects. So instead of related products we needed to have the string say related objects.

change strings without a plugin

To accomplish this, you basically just need to add the following lines of code to the file functions.php of your theme or child theme:

function gb_change_cart_string($translated_text, $text, $domain) {
    $translated_text = str_replace(“related products”, “related objects”, $translated_text);
    return $translated_text; 
} 

add_filter(‘gettext’, ‘gb_change_cart_string’, 100, 3);

In the above code the function gb_change_cart_string will hook into the function gettext and return the altered string. So instead of related products the website will show the string related objects. And actually the website does not have to be translated – this solution will also work even if you don’t use any translation – if you are working with the standard English installation. A simple solution that just works. In most cases.

Leave a Reply

Your email address will not be published. Required fields are marked *