Last Updated: February 25, 2016
·
1.483K
· isuruj

Search and Replace the localhost instances in a hosted WordPress installation

NOTE: This post is directed to students who took the Bootstrap to WordPress course Udemy course. But it can be applied for anyone who's in a similar situation.


From Section #17 onwards, it is assumed that you have your very own host and a domain to set up the WP theme you created. But if you're like me who doesn't own my own host and a domain, you can go for a free hosting provider like bytehost or biz.nf.

In Lecture #79, Brad discusses about the issue where you have instances of localhost:8888 in the database which causes the pages to redirect. And he introduces a script called Search-Replace-DB which we can use to easily search for those instances and replace with them with our current domain URL.

But if you're using a free hosting provider, you most probably won't be able to run that script due to restrictions imposed by them. That's where you have to go old school and use plain old SQL queries to accomplish this.

First do a search for the string 'http://localhost:8888' in the database. You'll see there are a little over hundred instances are there.

We have 3 tables which we need to take care of. Let's go through them one by one.
First the b2w_options table. It has 2 occurrences. Click on the 'Browse' link and if you take a look at the table below, you'll see those 2 instances are in a field called option_value. Remember that. Then select the 'SQL' tab at the top.

There's a big text area which you can run SQL queries. The basic SQL query we're going to be running is of this format.

UPDATE `table_name`
SET `field_name` = replace(field_name, 'unwanted_text', 'wanted_text')

Let's modify it to suit our need.

UPDATE `b2w_options`
SET `option_value` = replace(option_value, 'http://localhost:8888', 'http://your-domain.com') 

And click the little 'Go' button at the bottom right corner. After that you go back to the Browse tab and recheck if the string replacement worked. It should.

Now let's move on to the next table which is called b2w_postmeta. Select the 'Browse' link and check the table and you'll see that the field of interest is the meta_value field. Select the 'SQL' tab, copy and paste the below query and execute it.

UPDATE `b2w_postmeta`
SET `meta_value` = replace(meta_value, 'http://localhost:8888', 'http://your-domain.com')

Move on to the last table, b2w_posts. You know the drill by now. Browse and take note of the field name which in this case is called guid. Select the 'SQL' tab and run this query.

UPDATE `b2w_posts`
SET `guid` = replace(guid, 'http://localhost:8888', 'http://your-domain.com')

You should see the familiar rows affected message. Now go back and do another database-wide search for the 'localhost:8888' string. And you might see still have missed some. Sneaky little bastards.

In my case these instances were in that same b2w_posts table, just in a different field. The culprit is the field post_content. Run this query to sort it out.

UPDATE `b2w_posts`
SET `post_content` = replace(post_content, 'http://localhost:8888', 'http://your-domain.com')

Run the database-wide search one more time to see if we're in the clear.

It looks like we indeed are. Great! Now go and reload your site and check if all the pages are working.

Well, that's it. Easy peasy!