/, Intermediate, Tutorial, WordPress/Changing text in WordPress using the gettext filter

Changing text in WordPress using the gettext filter

I recently got a query about changing some text in our plugin (When Last Login) and this tutorial will show you exactly how to do this using the built in ‘gettext’ filter. We will be changing the word ‘Never’ to ‘User has not logged in yet’.

So first of all, I like to ensure that the text we are wanting to change is able to change using the gettext filter. It is good practice for plugin developers to wrap strings inside  _e() or __() which we will cover in another topic to get your plugin internationalized or known as i18n.

Inside When Last Login’s plugin folder, open ‘when-last-login.php’ with a text editor of your choice and search for ‘Never’. We can see that the plugin is able translate or changed the word ‘Never’ to whatever we would like because it is wrapped inside __(). You will notice that there is ‘when-last-login’ as the second parameter which is our text-domain. This just means that the word belongs to our plugin. This is for internationalization and so we can support multi-language formats.

when-last-login-never

Code from When Last Login – Searched for ‘Never’

Now that we have found the text is wrapped correctly we can change it to ‘User has not logged in yet’. Now to the good part –

Open your theme’s functions.php file and navigate to the end of the file (I am hoping you are using a child theme 🙂 ), next we will insert the following code:

What the above function does, is searches for the string ‘Never’ and then replaces it with the value of ‘$translated_text’.

You may see the WordPress codex for the gettext filter here – https://codex.wordpress.org/Plugin_API/Filter_Reference/gettext

 

 

2016-11-08T11:29:53+00:00By |Coding, Intermediate, Tutorial, WordPress|0 Comments

About the Author:

Addicted to WordPress. Plugin developer. Support Engineer. Follow me on Twitter @andrewlimaza for more WordPress related content.

Leave A Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.