Fixing broken images after moving WordPress

2015 EDIT: This applies only if you’ve moved your WordPress installation from one folder in your system to another.  If you’re actually migrating to another host (like from your own installation at WordPress.org to a managed installation at WordPress.com ) use the export and import feature instead. I’ll write about troubleshooting that in a new post, but you can also find info here and  here.

This advice applies to individual WordPress.org installations that have been moved to  a different directory on the server. In my case, I had moved my WordPress.org installation out of the “ideablog” directory and into my web root.  It’s really just a simple MySQL Find and Replace, but here you go.

A few days after moving this blog to a different folder, I realized all of my older posts had broken images. Luckily, the fix was a relatively easy find-and-replace in my favorite little MySQL client. Or even my unfavorite client. I used phpMyAdmin, and after making a backup of my database, it went something like this:

UPDATE `wp_posts` SET post_content = REPLACE( post_content, 'yoursite.com/OLDFOLDER/', 'yoursite.com/NEWFOLDER/' )

So, for example, if you blog was at cute.net/nyan and you’ve moved it to cute.net/ponycorns, and now you want to fix the broken image links, you would back up your database and run a query like this:

UPDATE `wp_posts` SET post_content = REPLACE( post_content, 'cute.net/nyan/', 'cute.net/ponycorns/' )

It’s just important to be sure to use enough of the old url that you’ll avoid replacing the wrong things. You wouldn’t want to replace ALL instances of the word nyan with ponycorn, right?

I hope that helps!