Replacing Text: Plugin or Theme

Being able to replace text in WordPress themes or plugins is very useful.

Here is Jeff Star on how to replace all instances of a string in WordPress. As he says, nothing is actually changed in the database — it’s only changed when output to the browser.

function replace_text($text) {
	$text = str_replace('look-for-this-string', 'replace-with-this-string', $text);
	$text = str_replace('look-for-that-string', 'replace-with-that-string', $text);
	return $text;
}
add_filter('the_content', 'replace_text');`

And this is all well and good, but assumes you know how to find the string and find it unambiguously.

Say What? Plugin

Being able to replace text in WordPress without being a developer is very handy, and bearing in mind the credentials of the author of the ‘Say What’ plugin author, feels safe. Lee Willis, the author is a WordPress core contributor. What this plugin does is enable you to replace text that is part of a plugin or theme with your desired text.

The free version is in the WordPress repository. What you need to do if you are using it is to go find the text string you want to replace. And that is not easy if you are not a developer, which is where we started.

The pro version of Say What will help you to find the original text with autocomplete once you start typing. And in the demo video of the pro version, it works very well. I am thinking about getting the pro version, which is $39.00 for a single site and $69 for multiple sites. The information on the site says that the cost for renewal will be 50% of the purchase price at time of renewal.

The only downside I can think of is that it really needs the plugin to be activated in order for the search and replace to work. It would be neat if it output a php snippet that one could add. I guess that would enable someone to buy a single site licence and then circulate it among multiple sites, but the relatively low cost maybe makes that unlikely. Still, I think it’s a good idea and I might mention it to the developer.

Export All URLs

Export All URLs and not all the things. Just export the URLs of all the posts, and the categories under which they were posted. Or if one wants, export the post IDs. As to when you would need this, well perhaps you are in my position.

I am going to move a site and change the platform of the main site. But I want to keep the WordPress blog directory. And I don’t want to reproduce or export-import all the posts. There is a lot of stuff in the 273 posts that I don’t want to move across. Some posts can just fade away, or I can 301 redirect them (but I probably won’t).

Export All URLs Plugin

Instead, I have copied most of the posts as plain text into a plain text document. And I made a note of the URLs. But then I will move the site and turn it into a completely WordPress site. At that point I will no longer have the old URLs to check. So I just used a neat little plugin in the WordPress repository named Export All URLs. I just ran it and it does what it says on the tin. Now I have a list of URLs and categories. And with that list I will be able check the URLs against my plain text posts. I will use that check list when I post the posts to the new site.

Eventually I will put a statement on the home page when the site is moved. It will explain that the whole service has moved to a new home. And it will explain all the good reasons someone should follow there. I hope all this is not premature, because the functionality of the new site is not in place yet. It too is built in WordPress with WooCommerce. But there will also be some custom templates and other goodies..

Find and Replace

Find and Replace is something that you really need when you need it. And the ‘when’ of when you need it is when you move your WordPress site to a new domain or server.

When you do that you will almost certainly need to run a search and replace on the database. The reason is that there will be links that are no longer correct. For example, if you have a link on a page or post that refers to a location on your old domain.

There are a couple of plugins that do Find and Replace. There’s Better Search And Replace, of which there’s also a pro version with added features. And there’s Search Regex made by John Godley, who also makes the Redirection plugin (which is excellent) and who works at Automattic.

Automattic is the company that owns the community version of WordPress and is the principal trustee for the version from which stand-alone WordPress sites are made.

Wordfence Lockout

Somehow or other, Wordfence locks you out of your admin panel. You can wait until the lockout expires. Or perhaps you have been permanently locked out.

You know the credentials, so you just need a way to get back in. One way is to FTP in to your server and rename the Wordfence plugin so that it is deactivated. But that’s not the best way because as soon as you reactivate it you will be back to square one.

Instead, VPN in to another location, log in and then go here and set these options:

Wordfence/Dashboard/Global Options/General Wordfence Options

Delete Wordfence tables and data on deactivation
Note: This does not include Login Security settings and tables. An option to delete those must be selected separately on the Login Security settings page.

Wordfence/Login Security/Settings

Delete Login Security tables and data on deactivation
If enabled, all settings and 2FA records will be deleted on deactivation. If later reactivated, all users that previously had 2FA active will need to set it up again.

Replacing Media

Enable Media Replace is a WordPress plugin made by ShortPixel that sells itself on the rhetorical question asking whether you find it tedious to have to delete a media file before uploading another replacement file with the same name.

The first option offered is to replace the file with a file of the same type and name. The second option is to replace the file with a new file with a new name, in which case the plugin updates all the links.

Deleting a media file before adding another with the same name is hardly an onerous task. But if I were to want to swap a file out with one of the different name, then I can see the value in the ability of the plugin to change the links. And if I had 50 files to swap out then I could definitely see the benefit.

From the ShortPixel site

Q: In Enable Media Replace the image isn’t replaced. Why?

A: Almost always the image/file is actually replaced when using Enable Media Replace (EMR) but because of cache the new image/file cannot be seen.
It’s very easy to check whether that’s your case by trying to directly access the URL of an image after adding “?anything” at the end of the URL. Please, note that after “?” you can add any random string you want and this trick is used to bypass any caching from your browser to the file.

On the Support page for the plugin I asked:

If I change media files using the plugin, is it then OK to delete the plugin, or does the plugin need to be active after using for the swap to continue working?

The developer replied and said “You can delete the plugin when you don’t need it anymore. When you replace a file, it is done forever.”

WordPress Duplicator Plugin

I just heard this mentioned on a Slack channel. WP Staging is a duplicator plugin that allows you to create a copy of your website. It creates a clone of your website into a subfolder of your main WordPress installation, including a copy of your database. And it does an entire search & replace of all serialised links and paths.

I am tempted to try it on a spare site just to see how well it works. As and when I do that I will report back here on how it went.