Changing text strings in WordPress

How to Fix Fatal Error: Maximum Execution Time Exceeded in WordPress
November 6, 2019
wordpress security
Securing a WordPress Site
July 24, 2020

WordPress, its themes and plugins all come with default or custom strings of text, ‘Read more’ for example. At some stage, as a site owner or developer, you will inevitably want to change the wording in one of these text strings and there are a few ways to tackle the task.

We recently had a customer who requested that we changed the message shown when no results were found for a search term on their site. The site uses the standard WordPress search function and a reasonably simple theme, but the no results text string very definitely wasn’t the standard string used by the WordPress search function – ‘No results. Try again, would you kindly?’. So where was the string we needed to change and how could we change it simply, without messing with theme or core WordPress files?

First job: Find where the text string is … Open every single php file in WordPress, the theme and the plugins and manually check down through the code? Not very time-efficient! Open phpMyAdmin and do a search in the database tables? Only possible if you have access to phpMyAdmin, the database login details and are comfortable with what you’re doing because the potential for breaking the site is very high. I could go on, there are numerous possibilites.

The answer to the problem for us was the ‘String Locator‘ plugin. Once installed and enabled, you simply choose which files you want to search through (the entire WordPress directory, or just the themes or plugins), enter the text you want to find and then off it goes. When it’s done, you are presented with a list of the files in which text string appears. In our case, the string appeared in the theme’s index.php file and a couple of files for language translation.

Wordpress text string search
Second job: So, we now needed to somehow change that text string in the index.php file. Easiest would be to simply edit the file. But that would be over-written when the theme gets updated. We could create a child theme and edit and save the php file there, but index.php is the main theme file and if we update the theme then our child theme version may well break things in future. These two options and various other code-change solutions were considered, but the fact that the string was found in translation files means that we could use a simple solution … use a translate plugin, which avoids the need to alter wordpress core, theme or plugin files.

Say What?‘ is a simple to use plugin that allows you to replace one string of text with another – that can be into a totally different language or just different words, whatever you enter. The only requirement is that the strings have been marked for translation, translated strings are generally wrapped in __(), _e(), or _x().

Install the plugin, activate and head to Tools > Text Changes. Click Add New and you’re presented with 4 text fields to complete.

Wordpress Text String Replace

The top and bottom fields are obvious, what you want to replace and what you want to replace it with. The tricky field is the Text Domain; the plugin needs to know where to look for the text string. So now we need to dig a little into the line of code where the string was found in index.php:

Wordpress Replace Text String in theme files

In the code above you can see the text string and following that is the text domain that ‘Say What’ is asking for; ‘hemingway’ … which in this case is the name of the theme, if the string is elsewhere, the text domain will be something other than the theme name. Note that the variables are wrapped in _e(), which means the content can be translated.

Back to the ‘Say What’ settings, enter the text domain.

Click the Update button. Test. Job done – we’ve changed a text string, without altering any files and the change will remain in place after any theme, plugin or WordPress updates.

Hopefully that’s helped one or two of you, feel free to comment below. Give us a shout if you need any WordPress help or custom work, we’re ready and waiting.

Jason
Jason
In 1999 I created my very first website, it all went from there. Founder of WP Manager (wpmanager.co.uk). WordPress Developer with a passion for technology and travel.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

Send this to a friend