Month: January 2013
WordPress – importing images from non-wordpress images directory
Working on a freelance site and somehow a bunch of images got uploaded to /images/2012/09/… what the heck? I cannot even figure out how that happened.
Trying this new “smush.it” plugin and guess what, it only works off images inside the media manager.
After looking around for a couple hours, I finally figured it all out. DISCLAIMER: Before trying this, make sure you back up your database. PHPMyAdmin has an “Export” option that will let you do this.
- First is a plug-in called “add from server” – http://wordpress.org/extend/plugins/add-from-server/. This plugin gives you a file manager type interface to browser your website directories, so I pointed it at /images and let it import from there. I wish it was recursive, but no such luck. Lucky for me, I only had to drill down into /images/2012/09/ and import from there as well as my top level /images directory. Importing gives you the option of using the original file date, so it copies the files to /wp-content/uploads/2012/09 and maintains that same date based hierarchy. From the /images directory I just imported those ones with today’s date, so they ended up in /wp-content/uploads/2013/01/…
- After importing I had to refresh some of my MySQL skills. The trick was to search the database for src=”/images/…” and update that to be src=”/wp-content/uploads/…”. It took me a couple tries to get it right. I started with a SELECT statement to get the syntax correct.
SELECT post_content,replace(post_content,’src=”/images’,’src=”/wp-content/uploads’) FROM `wp_posts` WHERE post_content like ‘%src=”/images%’;
This let me verify the substitution was happening the way I wanted it to, then it was just a matter of rewrite this as an UPDATE query (after I BACKED UP MY DATABASE!), this was the query:update wp_posts set post_content=replace(post_content,’src=”/images’,’src=”/wp-content/uploads’) WHERE post_content like ‘%src=”/images%’;