It is my pleasure to introduce to you an incredibly useful batch and bulk updating utility plugin for WordPress – TGM Batch Updates.
The TGM Batch Updates plugin is a batch and bulk updating utility for WordPress that allows you to update massive datasets with relative ease. It was built to allow you to quickly build batch queries and process massive amounts of data with the click of a button.
As you may or may not know, I use Easy Digital Downloads with the EDD Software Licensing extension for all of my products. Soliloquy is my oldest product, and as such, no licensing extension (nor EDD for that matter) existed when I started to sell it. Because of this, I had to create my own custom licensing system for Soliloquy. Thankfully the software licensing extension came along, and I was finally able to migrate over to a license system that I knew would be updated and supported because Pippin and the team had a vested interest in keeping it running.
The migration to that system, however, has not come without its issues. Most recently, an update to the Software Licensing addon required an internal upgrade. I backed up my site and processed the upgrade, but little did I know that my old license key format would not be compatible with the update. This left me with nearly 10,000 legacy keys that suddenly stopped working for automatic updates.
Talk about serious panic.
I decided right then and there that I did not want to roll the updates back and perpetuate the issue, so I started looking into ways to handle batch updates. Doing this manually was not an option (and the interface for doing it manually didn’t exist anyways), so I decided to take the time and build a utility for doing this for me. Viola – TGM Batch Updates was created.
What does this thing do?!
Glad you asked. I’d love to explain.
The TGM Batch Updates plugin is just like any other normal plugin. You can install it through the Plugins interface or via FTP. Once installed and activated, a new menu items under the Tools menu will be added called “Batch Updates”. Click on this link and you will be taken to your simple batch updates page.
There is not much pizzaz with this page – just a simple form that lets you set an upper limit on the number of potential items to process (defaults to 1000) and a button that says “Start Batch Updates”. The power behind this is what’s inside the plugin. You can browse the code below.
As I said previously, this plugin is for developers. You must edit the plugin yourself and fill in the relevant query processing details. Every situation will be different, so this plugin is meant as a general starting point with the powerful ajax functionality built into it to handle the batch updates.
You should only ever modify three things in the plugin (and they are conveniently located at the top of the class): the
$num property, the
get_query_data method and the
process_query_data method. The
$num property delineates how many items you want to process per each batch request. It defaults to 10, but you can experiment with this to do it in quantities of 25, 50, 100 or even more. It all depends on what your server can handle and how intensive your data processing will be.
The get_query_data method runs the query against the database to retrieve the data that will be processed. The offset and posts_per_page parameters are handled for you by the plugin – it’s up to you to figure out what type of query you want to run. Create whatever type of query you would like – you can use the WP_Query codex article as a guide.
The process_query_data method is the meat of the plugin and actual manipulates the data retrieved from the database. By default, it loops through posts and checks if a custom field exists. If it doesn’t, the custom field is added with the post title as the value. This is where you need to modify and change as necessary to fit your needs.
Everything else in the plugin should be pretty much off limits. The batch update process is handled by ajax requests based on your batch limit and number of items per each batch request. It will give you an estimate of how many steps it will take to finish the process (but could be less if the amount of data you have is less than the limit).
I’d love to know your use cases and how it has helped you. Also, if there are any improvements, feel free to add them as Issues on the Github page. Enjoy batch updating in WordPress with TGM Batch Updates – it doesn’t get easier than this!