When you find a broken link on a website, you may want to replace this broken URL with another one. If you have used the URL only in a single post or page, you can change the link in a few seconds. But what if the URL is present in multiple posts .. Let’s say 100 posts. What would you do in this case?
There are two ways to modify the link – one way is to run a MySQL query, and another way is to use a plugin that enables you to find and replace the text in the WordPress database. Below, we’ve covered the two methods:
Note: Back up the WP database before running the replace function of the plugin or running the MySQL query we have shared below.
MySQL has a built-in function to change the text in the database table of your choice. The function we’re talking about is replace(). Here’s the query that will let you change the text in any table of your choice:
update db_table set column_name = replace(column_name, 'orginal_text', 'new_text') where ID= XYZ;
In the above query, change db_table with the name of the WordPress table that has the text, and replace column_name with the name of the table column that contains the text that you want the query to alter. For example, if the table is wp_posts, change column_name with post_content where post_content is the column where the WordPress content management system saves the post or page content. Now replace original_text with the text you want the MySQL query to find in the entire column row and new_text with the text you want MySQL to replace. Now change XYZ to the post/page ID. The ID can be a primary key.
What will happen when you run the above query? MySQL will alter the text (or URL) in the post_content column of the row with an ID that you specify.
Using a plugin
Better Search Replace is the oldest and most popular plugin for finding and replacing words or URLs in the database table. Once you activate the extension after installing it, move the mouse cursor on the “Tools” menu of the WP dashboard and click the “Better Search Replace” option. Now, you’ll see a form with a button, three checkboxes, and three textboxes. In the first text box, enter the phrase or word you want the BSR plugin to find, and in text box 2, enter the word that you want the extension to replace the text in text box 1 with.
Now select the database tables that you see in a list and click the button that you will find at the bottom of the form. Don’t deselect the “dry run checkbox”. Once you click the button, the plugin will show the number of entries that will be modified in the table/tables you’ve selected. When you’re satisfied with the dry run’s result, re-fill the form, deselect the “dry run” checkbox and click the “run Search/Replace” button. That’s it! Your URL or text would have been replaced by the extension. To check whether the plugin has done your job, open the page where the old URL/text was present.