How to modify text strings in plugins and themes

Most plugins and themes provide filter hooks that allow you to modify strings like titles and labels. However not all do. Luckily there is a filter hook you can use to customise these strings.

Plugins or themes that allow any level of customisation usually have strings passed through filters that provide a chance to manipulate the text before it is retrieved or printed out.

For example, in WooCommerce the “Add to cart” text in buttons is passed through a filter “woocommerce_product_single_add_to_cart_text”.

apply_filters('woocommerce_product_single_add_to_cart_text', __('Add to cart', 'woocommerce'), $this);

To change the text to “Buy now” you would simply need to add a filter and return a new string.

add_filter('woocommerce_product_single_add_to_cart_text', function ($text) {
	return 'Buy now';

Unfortunately, not all strings are passed through filters and in some cases, you would have to modify a template file just to change one word or sentence. This is not ideal since you might miss on new features that are added in future updates.

An alternate method is to leverage the localisation options that a plugin or theme may provide to enable translation into other languages.

The gettext filter hook is applied to all translatable text that is passed through localisation functions like __() or _e() – These functions allow themes and plugins to be translated via .PO files or translation plugins. By leveraging this hook we can change the text of a particular string even if no internationalisation is in effect.

In the example below the “Related Products” heading in WooCommerce is changed to “You might also like”.

add_filter('gettext', function ($translated_text, $text, $domain) {

	if ($domain == 'woocommerce') {
		switch ($translated_text) {
			case 'Related products':
				$translated_text = __('You might also like', 'woocommerce');

	return $translated_text;

}, 20, 3);

Faced with the task of changing a text string, it is very frustrating to learn that no hook or option was included with the plugin or theme. Fortunately, this method provides a simple solution that can be used in many situations.

Recent Articles