How to Modify Text in a WordPress Plugin or Theme Without Another Plugin

We’ve all been there, you’ve found the perfect theme or plugin for your site, but the wording on that button just isn’t right. Or maybe you need to change the word Cart to Basket in WooCommerce. Whatever it is, you’ve probably come across plugins that will change the text for you, but what if you want to do it without having to install another plugin?

One thing’s for certain – you shouldn’t go editing theme and plugin files directly. Any changes you make will be overridden when you update! There is a solution though, and all you need to know is the “text string” and a simple snippet.

What is a text string

Okay, so you may be asking yourself “What is a text string”? Simply put, a text string is a piece of code that the plugin or theme developer has used to allow you to change some text. Let’s look at the WooCommerce plugin as an example. You may want to change the word “cart” to “basket”. If you looked through the WooCommerce code for the word “cart”, you would come across some code that looks like this:

__( 'cart', 'woocommerce' )

This is a text string, and it basically means that you can change the word “cart” to whatever you want. It works with sentences too, another example of a text string in WooCommerce is:

__( 'Shipping options will be updated during checkout.', 'woocommerce' )

This text string means that you can change the sentence “Shipping options will be updated during checkout.” to whatever you want.

Some text strings have a “context”, which means that a certain word or phrase can be changed depending on where it is used. Again, looking at WooCommerce, here’s an example of a text domain with a context:

_x( 'On hold', 'Order status', 'woocommerce' )

Notice that a text string with a context starts with _x

Finding the right text string can be a bit of trial and error, but the easiest way to do it (without installing another plugin) is to download a copy of your plugin to your computer and use a program like Notepad++ to search through the plugin files for the text in question.

Modifying the text

Once you’ve found the text string, it’s just a matter of changing the text. You just need a simple snippet for that. In the example below, we’re going to translate the word ‘cart’ to ‘basket’ in WooCommerce. The text string in question is __( 'cart', 'woocommerce' )

add_filter( 'gettext', 'snippetpress_change_text', 10, 3 );

function snippetpress_change_text( $translation, $text, $domain ) {
	if ( $domain == 'woocommerce' ) { // Replace woocommerce with the text domain of your plugin
		if ( $text == 'cart' ) { // Replace cart with the word you want to change
			$translation = 'basket'; // Replace basket with your new word
		}
	}
	return $translation;
}

If our text string also has a context, we need to use a slightly different snippet. In the next example, we’re going to change the text “On hold” to “Order received” in WooCommerce, where it has a context of “Order status”. The text string in question is _x( 'On hold', 'Order status', 'woocommerce' )

add_filter( 'gettext_with_context', 'snippetpress_change_text_with_context', 10, 4 );

function snippetpress_change_text_with_context ( $translation, $text, $context, $domain ) {
	if ( $domain == 'woocommerce' ) {
		if ( $text == 'On hold' && $context =='Order status' ) {
			$translation = 'Order received';
		}
	}	
	return $translation;
}

Where to add the snippet?

Whichever snippet you choose to use, you should place it at the bottom of the functions.php file of your child theme. Make sure you know what you’re doing when editing this file. Alternatively, you can use a plugin such as Code Snippets to add the custom code to your WordPress site. If you need further guidance on how to add the code, check out our post on How to Add WordPress Snippets.