I am hoping that someone can shed light on this problem I am trying to solve, even if simply changing my thought process as to how I could solve this.
I am importing a custom CakePHP based CMS web application into WordPress by means of traversing the database, making numerous SQL queries, building arrays and finally outputting the necessary XML file for import using the WordPress import tool.
Currently I have managed to import categories, tags, and posts / pages just fine, no hiccups, however it is the images that seem to be the issue. The current format of images in the CakePHP based CMS is images/year/month/day/post_id/image_name.ext
As far as I understand, but default WordPress can only store media inside wp-content/uploads or wp-content/uploads/year/month
What I have done is copied over all the image folders from the CakePPH app directory into my WordPress wp-content/uploads directory, so an example image will have this path: http://localhost/wordpress_site/wp-content/uploads/2013/01/01/12345/image.jpg
Inside my WordPress XML file that I am generating, I have set the attachment_url as well as GIUD to be as it is above, referencing the new image I place inside my uploads directory. I even set the file path in the serialzied meta data of the attachment element in the XML.
If I run the import process and DO NOT check the ‘Download and import attachments’ option in WordPress, I get errors that the media cannot be imported…
If I DO check the ‘Download and import attachments’ option, the images ARE located inside the custom path I specified above, but they are resized according to the thumbnail dimensions etc, and the image is still put inside a year/month/image.jpg path. So all WordPress did was read the file at that (custom) location, but still set the final path to a standard WordPress one, moving the image and its generated thumbnails over and away form the desired location.
Does anyone know of a way to simply have the media in a custom path inside the uploads folder AND force WordPress to accept and use that path when running an import?
I understand that via actions and hooks I can change the upload path structure, but only within WordPress and only when uploading media via the post / page / media manager, not during an import. The last thing I want to do is hack the import script.
Any help, guidance, words of advice will be more than appreciated.
Yes you can by programatically adding the attachment.
This will tell WordPress that there is an attachment at $target and you can optionally attach it to a post with the last parameter in the call to wp_insert_attachment.
To everyone who faces the same dilemma as I did, there are actually a lot of online resources dictating how this issue can be solved. I found the best way to find the solution was actually to search for something like Import WordPress attachments programatically, which churned up a fair amount of solutions.
Thank you to @tobbr for his valuable guidance in pointing me in the correct direction. My final solution involved creating a plugin, and then traversing the old sites (CakePHP) database for old images and their paths, then using those to import the attachments into WordPress.
The important key point to take into consideration here is that I CLONED the old sites images directory into my WordPress uploads directory. The old image paths were as follows:
CAKE_ROOT/app/WebRoot/images/2012/10/2/12345/image_name.jpg
Where 12345 = the ID of the image entry in the database (which would become the ID of the attachment when importing into WordPress. So I moved the ENTIRE images folder above into my WordPress uploads directory, so my WordPress image folder structure looked like follows:
WP_ROOT/wp-content/uploads/2012/10/2/12345/image_name.jpg
It is important to note that before you even begin to import attachments programatically, your images MUST live somewhere (anywhere) within your WordPress uploads directory. Also make sure you have the necessary permissions, for the sake of the import I ran a
chmod -R 777
command on my uploads folder before starting.I then wrote the plugin, which was not nearly as complicated as I imagined, so here it is for anyone needing to do a similar task:
AND HERE IS A BONUS FUNCTION:
I came across this AFTER writing the plugin, however if you only have a few (I had over 100,000 images to import!) there is a VERY handy function in WordPress called media_sideload_image which grabs an image from a remote URL and attaches it to a post of your choice, pretty worth taking a look at. Get more info at the codex: http://codex.wordpress.org/Function_Reference/media_sideload_image
Some links to WordPress Codex resources that helped me are as follows:
Inserting attachments programatically
Other external sites that aided me:
I hope this helps someone as it did me.
Simon