Run Image Attributes Pro Bulk Updater Programmatically / via Cron Job

Image Attributes Pro updates image attributes such as image alt text and image title using it’s bulk updater. The bulk updater is designed to cycle through each image on your WordPress website and update it’s attributes.

The bulk updater is housed in the Bulk Updater tab of Image Attributes Pro and runs using Java Script as long as the browser tab is kept open.

Bulk_updater

If you have a use case where you need to run the Bulk Updater programmatically instead, the following function is for you.

/**
 * Run Image Attributes Pro Bulk Updater Programmatically
 * 
 * Note: Requires Image Attributes Pro version 4.0 or above.
 * 
 * @param (int) $batch_size Defines the number of images that will be processed
 * in a single call to the function. Higher batch sizes can lead to PHP time outs. 
 * 
 * @link https://imageattributespro.com/run-bulk-updater-programmatically/
 * @author Arun Basil Lal
 */
function prefix_iap_run_bulk_updater( $batch_size = 20 ) {

	// Return while call is made from ajax.
	if ( wp_doing_ajax() ) {
		return;
	}

	// Return if Image Attributes Pro is not active.
	if ( ! function_exists( 'iaff_is_pro' ) || ! iaff_is_pro() ) {
		return;
	}

	// Return if all images are updated. 
	if ( iaff_count_remaining_images() === 0 ) {
		return;
	}

	/**
	 * Action hook that is fired at the start of the 
	 * Bulk Updater before updating any image.
	 * 
	 * @link https://imageattributespro.com/codex/iaff_before_bulk_updater/
	 */
	do_action('iaff_before_bulk_updater');

	// Process one batch of images. 
	iaffpro_bu_run_bulk_updater( $batch_size );

	/**
	 * Action hook that is fired at the end of the 
	 * Bulk Updater after updating all images.
	 * 
	 * @link https://imageattributespro.com/codex/iaff_after_bulk_updater/
	 */
	do_action('iaff_after_bulk_updater'); 
}

Things to note:

  • $batch_size determines the number of images processed in one call to the function. The value in the above example is 20 which means that 20 images will be processed for every call to the function.
  • A higher $batch_size means more images will be processed at one. But it will also increase the chance of a PHP time out. Use with caution.

Run Image Attributes Pro Using Cron Job

You can add the above function in a PHP file / script and call the file as a cron job. Remember to load WordPress in that case.

For example, if you are going to add the PHP file to the root folder of your WordPress installation (the same folder with wp-config.php), then you can load WordPress by adding require( 'wp-load.php' ); at the top of the file.

Here is the file with all the necessary code.

Adding the file to the root folder and then calling the file via a cron job will run Image Attributes Pro programmatically and update the image attributes.

Was this article helpful?
Yes, thanks! 👍Not really 👎