How to avoid timeouts when importing a WordPress export XML file
WordPress provides a great mechanism for exporting and importing data simply by using Tools > Export or Import. However when used on large datasets, as well as media files that take time to import, you’re bound to run into timeout issues.
Obtaining an XML export from the source
If you’re exporting from another self-hosted WordPress site, you can learn how to obtain your XML export file from the source site here. If you choose specific content (like posts) and there are images in your content, you will not be able to transfer them without also making an export of type “Media”
How to safely import the XML file at the destination
When importing that file on the destination site under Tools > Import, there are three ways to ensure it imports smoothly:
- Learn how to adjust your PHP timeout values temporarily so that you can avoid Gateway Timeout errors, then head to Tools > Import and import the file. Do not forget to set the timeouts back to their original values post-import to avoid performance issues on the site.
- Use the WP All Import plugin – it batches the data import so that timeouts are not likely to be reached (this is what the built in WordPress import function should do but doesn’t).
- Use the wp-cli utility via an SSH connection to avoid front-end timeouts. Server timeouts are only configured through the web server configuration, and so are not present when importing via command line with wp-cli.
Note regarding Option 1: it can be tricky to estimate how long you need to set the timeout for. If you have more than, say, 100-200 media files in WordPress, it’s recommended to use one of the other two options.
Options 1 and 2 are simpler, while Option 3 (wp-cli) is more robust, but more technical knowledge is required to use it. If you’d like to give wp-cli a try, read on!
Using wp-cli to ensure a reliable import
WP-CLI is a command line method of working with WordPress. Rather than importing using the WordPress admin GUI, you do so when logged in via SSH which bypasses nginx, apache, fastcgi, and php timeouts since it doesn’t use anything but straight-up PHP to run.
This following steps are specific to running this on a Plesk server. It is an advanced guide because you must be familiar with connecting to your server via SSH and using the command line to use it.
- Learn how to install and use WP-CLI on a Plesk server
- Upload the file using either SFTP or Plesk File Manager (it’s simplest to upload the file to the domain’s web root folder. How to find the web root is described in both of those guides).
- Run the import command:
php wp-cli.phar import [my_filename].xml --authors=create
If you have multiple XML files (like one for posts and one for media), repeat the import command for each of them.
If there is any problems during the import (like the process is killed) you can always re-run it. In all cases where I’ve had to do this, the import utility has skipped over existing imported items and resumed where it left off.
That’s it! This guide shows you how to import your WordPress XML exports by using CLI to avoid web server timeouts.
Posted in Advanced, Code, Tips and Tricks
About Websavers
Websavers provides web services like Canadian WordPress Hosting and VPS Hosting to customers all over the globe, from hometown Halifax, CA to Auckland, NZ.
If this article helped you, our web services surely will as well! We might just be the perfect fit for you.
Thanks. This forced me to learn wp-cli as I had a complex import that kept failing and now it appears to just be running and a great resource for future projects 🙂
You’re welcome! Glad to have helped 🙂
i love you!
In my case I just had to restart the import of the same file 4 times until it started the import again (it skips uploads already imported). Annd worked like a charm.
Hey Rafael,
Great to hear! Are you referring to restarting the import via the WordPress Import UI tool or using the command line? If you’re referring to the UI, I had also noticed that it skips existing items, however the last time I tested it, it had actually duplicated media content every single time I ran it 🙁
Perhaps that’s fixed now?
Ultimately I’d love to see the WordPress import utility allow batching so that it can run each batch quicker than standard 45s timeouts without having to re-run things.
Is there a way to do this on the CPanel
Hey there, using the command line to import with wp-cli should be doable on any platform! You’ll just need to find which PHP path to use for the binary if cPanel has multiple PHP versions installed.
How to automate the above process? like I am creating a plugin using REST APIs to get product from main store and insert it into other stores or remote servers, the plugin can be use by any person without prior settings on their server.
Hey Irshad,
REST APIs operate through HTTP, so there shouldn’t be a connection to what you want to do and this method of importing XML files from command line. No matter what happens a REST API is going to need to be received by a web server and thusly is subject to that server’s timeout values. That said, you *can* either:
1. Batch the data stream into smaller segments so that it’s never likely to exceed any given server’s default timeout value, or
2. If you have access to the server, simply change the HTTP server and its php timeout values