Patrick's Programming Blog

gettext – The Most Useful Filter in WordPress

camera filter

So you've found the perfect plugin: it has all of the functionality you need, it's within your budget, and there's surprisingly awesome support. Great – but what if you need to change just one little line of text? What if you want to do something simple like change “Add to Cart” to “Add to Basket”? Or change “Related Products” to “See these related products”?

If you're lucky there's a filter for that particular string but sometimes there isn't. Then what do you do?

Codestyling Localization

You could certainly use something like Codestyling Localization which is nice for people who has no coding ability but do you really want to have to edit the .po file every time a new version of the plugin comes out? Of course not.

Cue the gettext Filter

This is where the incredibly useful gettext filter comes in. If you take a peek at your plugin's code you'll see strings like this:

You can easily translate these strings by adding a filter to your functions.php file:

Easy huh? You can add as many translations to that filter as you need.

Final Result

With just 15 lines of code I was able to translate several strings on the WooCommerce single product page.

Using just 15 lines of code I translated three separate strings on the WooCommerce product page.

After seeing these examples do you have any doubt that the gettext filter is one of WordPress' most useful functions?

Image credit: Wikiepedia.org

Exit mobile version