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.

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’.