wpblogbig

The problem

Recently we came across an issue whilst moving a blog from a wordpress.com site to a self hosted WordPress site where the ID’s within the gallery shortcodes no longer matched with the relevant attachments. This meant that the wrong images were showing within the galleries!

When you import any type of content into WordPress it’s given a unique ‘Post ID’. This includes images, or ‘attachments’ as they’re known in WordPress. When you insert a gallery into a WordPress page or post it creates a shortcode such as [ gallery ids="1508,1507" ] with the ‘ids’ being the unique ‘Post IDs’ of the attachments you’re including within the gallery.

The problem comes about when importing into a WordPress install that already has existing content. During the import WordPress will try to use the same ‘Post IDs’ as those from the previous site, however if that ID is already in use by something else, a new ID is assigned to it. For example if it’s trying to import an attachment with the ID 1234 but that’s already in use, WordPress will give it the next available ID which could be anything, for example 6789. When a post containing a gallery is then loaded, say [ gallery ids="1234" ] it won’t find the correct image any more.

The solution

One obvious solution is to make sure you do any importing into a clean WordPress install, that way you’re unlikely to come across any already in use ID’s.

In our case we already had a development site up and running, full of new content. The solution we came up with was to write a script that automatically updated the imported posts with the new attachment ID’s. This script is run after the standard import has been completed. The script works as follows:

  1. Load the same XML file used from the wordpress.com export and loop through each individual item (post)
  2. Look through the post content for references to the gallery shortcode and extract the id’s from it. Store these ID’s and the post’s titles to loop through later.
  3. Whilst looping through the posts, store the ID’s and post_name’s of each imported attachment
  4. Once the above is done, start a new loop for each of the posts that you’ve found galleries for
  5. Take the gallery ID’s and for each one, using the stored attachments found in the import, look on the new site for the attachment with the same name. This will give you the new ID for that attachment
  6. Using the title of the imported post, load the imported post from the new site and find the references to the galleries within its content
  7. Replace the old attachment ID’s with the new ones
  8. Save your changes to the post

The code/plugin

We’ve added the code/plugin used below for your reference, before you use it please be aware of the following:

  • This code was created for our specific use case, it may need modifying to suit your needs
  • The code should only be run once!
  • The code is provided as is and should only be run by those that understand it and know what they’re doing
  • You should only use this in a development environment and please ensure you have a full database backup before hand!

How to use it:

  • Install and activate the plugin
  • Upload your XML file to the media library or through FTP and copy the file location
  • Go to the new WP Dashboard menu item called ‘bweb gallery IDs fix’
    plugin-2
  • Add the XML file location and click ‘Save Changes’
    plugin-1
  • Click on the link in the text to run the script (only do this once!)
  • Deactivate the plugin and remove the XML file

You can download it here

We hope you find this useful, please feel free to use/modify the code as you see fit.

Get our news direct to your inbox

Subscribe to our mail list