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, you’re bound to run into timeout issues, especially when that data includes images since it takes time for the importer to grab those images from their source and download them into the destination site.

While most hosting providers (including us on Titanium plans) allow you to adjust the PHP timeout values to increase them to your liking, there are additional timeouts that are not adjustable. These permanent timeouts on shared hosting help ensure that if there are ever any runaway scripts that aren’t being monitored, they will not impact performance for any great length of time. But of course, they can be problematic to deal with when you are monitoring something and want to import data that will take quite some time.

The solution: wp-cli

WP-CLI is a command line method of working with WordPress. Rather than invoking the import command using a URL, 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 guide is 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.

1] Get your Export File

Download export file from source site by logging in to the WordPress admin then choosing Tools > Export. You can download all content, or just specific content if you wish by selecting the type of content you wish to export.

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”

2] Upload export file in Plesk

  • Login to Plesk
  • Find the domain you wish to import to, then select “Files” to go to the Plesk file manager. You should be looking at the WordPress files like wp-config.php and the wp-content folder.
  • Drag and drop the WordPress XML export file into the file manager to upload it.

You can, of course, also upload via FTP/SFTP if you prefer that method.

3] Set up Environment & Import

Login via SSH and change to the directory you just uploaded the XML file to, then run the following.

Note that you can replace /opt/plesk/php/7.0/bin/php with straight up php if you wish to use your operating system’s stock PHP version. You can also replace it with different Plesk PHP versions simply by swapping out the 7.0 with the version of your choice, like 5.6, 7.1, or 7.2, but you must (of course) have that version installed using the Plesk installer on your server. We have version 7.0 installed from the Plesk installer on all of our servers.

USER=[put your SSH username here]
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chown $USER:psacln wp-cli.phar
/opt/plesk/php/7.0/bin/php wp-cli.phar --info
sudo -u $USER /opt/plesk/php/7.0/bin/php wp-cli.phar --authors=create import <xml_filename>

If you have multiple XML files (like one for posts and one for media), repeat this 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.

Source for installing wp-cli

Jordan is a computer, security, and network systems expert and a lover of all things web and tech. Jordan consults with project management for software companies. Jordan is a founder and managing partner at Websavers Inc.

6 Comments

  1. Rafael Alfaro on October 8, 2018 at 4:01 pm

    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.

    • Jordan Schelew on October 8, 2018 at 8:47 pm

      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.

  2. kike on August 1, 2018 at 12:22 am

    Is there a way to do this on the CPanel

    • Jordan Schelew on August 7, 2018 at 12:05 pm

      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.

  3. Irshad on April 5, 2018 at 5:19 am

    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.

    • Jordan Schelew on April 6, 2018 at 5:33 pm

      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

Leave a Comment