1

I have this scenario: about 200 photo galleries to recreate in Wordpress from an old wordpress site.

All I have is the .xml WXR for the post_type "photogallery" that basically creates the gallery and attachs to it a bunch of image ids.

Unfortunately I can't export the "complete XML" (with images, ids and attached posts) nor access the dB.

So basically I should create a script that:

Reads the exported XML, retrieve the original URL for the gallery (the old site is still alive) open the URL, get the image in a specific and the text in and load it in wordpress, using the text as the image description, and associate it with the post_id found in the XML, open the link for the next image in the gallery (for example simulating a click over "next" button), and loop until all the images in the gallery have been loaded (average 10 to 15 for each gallery).

Any suggestion?

1 Answer 1

0

How will you represent the galleries on the new site? Will each gallery be a post with a standard [gallery ids=...] shortcode?

If so, you'll basically only need the url for each image on the old site (plus post heading/text and image descriptions if needed), and you can create a WXR import file to import everything into the new site.

Example, an import file with a gallery with two images (tested on WP 4.2.2):

<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0"
    xmlns:excerpt="http://wordpress.org/export/1.2/excerpt/"
    xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:wfw="http://wellformedweb.org/CommentAPI/"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:wp="http://wordpress.org/export/1.2/" >

    <channel>
        <wp:wxr_version>1.2</wp:wxr_version>

        <!--A post with a little text and of course a gallery of images-->
        <item>
            <title>My first gallery</title>
            <dc:creator><![CDATA[admin]]></dc:creator>
            <description></description>
            <content:encoded><![CDATA[
<p><b>Welcome to my first gallery!</b> Here are the images:</p>
[gallery ids=301,302]
]]></content:encoded>
            <excerpt:encoded><![CDATA[]]></excerpt:encoded>
            <wp:post_id>300</wp:post_id>
            <wp:post_date>2015-03-05 16:20:00</wp:post_date>
            <wp:status>publish</wp:status>
            <wp:post_parent>0</wp:post_parent>
            <wp:post_type>post</wp:post_type>
        </item>

        <!--The images attached to that post-->
        <item>
            <title>Picture of a cat</title>
            <dc:creator><![CDATA[admin]]></dc:creator>
            <description></description>
            <excerpt:encoded><![CDATA[This is a picture of a cat I found]]></excerpt:encoded>
            <wp:post_id>301</wp:post_id>
            <wp:post_date>2015-03-05 16:21:00</wp:post_date>
            <wp:status>inherit</wp:status>
            <wp:post_parent>300</wp:post_parent>
            <wp:post_type>attachment</wp:post_type>
            <wp:attachment_url>https://upload.wikimedia.org/wikipedia/commons/f/fc/Minka.jpg</wp:attachment_url>
        </item>
        <item>
            <title>Picture of a dog</title>
            <dc:creator><![CDATA[admin]]></dc:creator>
            <description></description>
            <excerpt:encoded><![CDATA[]]></excerpt:encoded>
            <wp:post_id>302</wp:post_id>
            <wp:post_date>2015-03-05 16:22:00</wp:post_date>
            <wp:status>inherit</wp:status>
            <wp:post_parent>300</wp:post_parent>
            <wp:post_type>attachment</wp:post_type>
            <wp:attachment_url>https://upload.wikimedia.org/wikipedia/commons/b/b7/Langhaardackel_merlin_2005.jpg</wp:attachment_url>
        </item>
    </channel>
</rss>

This is close to the minimum of info required to do an import, but there are many other settings that can be added in the <item> elements if you need them, for example categories.

A few things to note:

  • Titles (<title>) must be unique across all posts and attachments, or Wordpress will skip them during import.
  • I believe a post must come before its images in the import file.
  • Keep track of your IDs. To hook up each image to their corresponding galleries, you must set each post and image ID (<wp:post_id>) yourself.
    • In the [gallery ids=...] shortcodes, you need to insert the images' IDs.
    • Each image should be registered as an attachment to its gallery post (<wp:post_parent>).
    • .. so start with an ID that is larger than any current post/attachment ID on the new site and count your way up (see where the IDs 300, 301 and 302 are used in the example).
  • <wp:attachment_url> is the url on the old site, where Wordpress can fetch the images from.

Once you have a complete import file, go to Tools > Import > Wordpress

  • (You may have to install the "Wordpress Importer" plugin if you don't have it already)
  • Choose your file and click "Upload file and import".
  • On the next screen, map old user to new users (in my example, all items were <dc:creator>-ed by "admin", so I just map them to the new site admin here), and most importantly, check "Download and import file attachments", to make the new site download the image files from the old site (via the urls in <wp:attachment_url>).

Import Wordpress

4
  • Yes, this is the plan. Do you have a solution . Commented Jul 11, 2015 at 7:34
  • Yes, I have added an example.
    – Sphinxxx
    Commented Jul 11, 2015 at 23:18
  • Ok thanks. It should work. The problem is that I have thousands of images. More than 200 gallery with 10 to 20 images each. So I do need a script that basically should create the xml. Starting from the WXR, it should crawl each gallery url, retrieve information parsing the html page (that part is easy) store the information in the xml file, simulate a click to open the next image of the gallery and keep going. The theory isn't that hard. But I'm not properly skilled. Commented Jul 12, 2015 at 19:38
  • Ok. You could start by looking into web scraping/crawling to to download all content (at least the html) to your computer, and then structure stuff to create the WXR from there. For example, have a look at Scrapy
    – Sphinxxx
    Commented Jul 12, 2015 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.