Working with imports or exports can be hard, especially on really large databases. WP CLI is a tool that helps you a lot. It can also help you with running imports or exports. If you expect large imports or exports, you can also create a WP CLI command for batch processing the data.

For doing batch imports or exports, you need a method with steps. For this tutorial, I will use the Easy Digital Downloads Batch import as an example.

You can find the code in their repository and you can also find an implementation for importing Downloads.

Creating the Command

Before creating the command, we need to make sure that we have the WP CLI installed.

Go ahead and activate the plugin. Now we will create the commands. If you try to run wp help you might be able to find batch there. If the class is without any public functions, that might not show up. So let’s do that now.

Generic Import Method

We will now create a generic import method that we can use for all kind of imports that are based on the class EDD_Batch_Import.

This method is not available as a WP CLI command since we are going to use it internally. We are making sure at the beginning that our options are set, at least the defaults (just in case other methods do not provide them).

After that, we set the step and per_step attributes so we can process the correct step and the correct number of data. Once that is set, we call the method process_step. This method will return true or false. If it’s true, it will mean that we have more data to process in the next step.

Method to Batch Import Downloads

Now, let’s do the batch importing of downloads.

We are making sure that we have the options (again). We will use those options for batch importing at the end of the method.

Since we are importing downloads from a CSV, the first argument in $args will have to be a path to the CSV file. To use the previously defined method process_import we need to pass an object. This object will be instantiated from the class EDD_Batch_Downloads_Import.

We then pass all the arguments, options and our object to the method process_import and wait for the return. If we have more to import, then we call this same command using WP_CLI::runcommand and passing all the options also so we get to increment the step through code.

You can read more about that command on the CLI handbook. There are a few options, but one in particular is interesting and that is the option launch.

We can run the command now with wp batch import_downloads PATH_TO_CSV.

Testing the Memory Usage

In a different WP CLI command that I’ve built for exporting coupons, I have also used a method to test the memory usage. I’ve copied the function from the php.net and transformed it into a method.

You can implement it before the above method calls itself to test various usages. If the option launch is set to false, it will use the existing process, otherwise it will start a new one.

For my test case, I’ve created around 35 000 coupons in WooCoommerce and here are my results using a similar approach for batch exporting:

  • Exporting 35 000 coupon all at once – memory peak usage: 228.5 MB
  • Exporting 35 000 coupons with launch set to false – memory peak usage: 224.5 MB
  • Exporting 35 000 coupons with launch set to true – memory peak usage: 130.5 MB

Conclusion

WP CLI is a powerful tool and creating batch imports or exports can be really useful here. With options such as launch, we can handle memory usage on a server much better and have a real batching system in place.

Have you tried using WP CLI before and create custom commands?

Become a Sponsor

Posted by Igor Benic

Web Developer who mainly uses WordPress for projects. Working on various project through Codeable & Toptal. Author of several ebooks at https://leanpub.com/u/igorbenic.

Leave a reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.