Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Run Image Attributes Pro Bulk Updater Programmatically
<?php
// Load WordPress.
require( 'wp-load.php' );
// Run the Bulk Updater
prefix_iap_run_bulk_updater();
/**
* Run Image Attributes Pro Bulk Updater Programmatically
*
* @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 = 10 ) {
// 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;
}
if ( iaff_count_remaining_images() < $batch_size ) {
$batch_size = iaff_count_remaining_images();
}
/**
* 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.
for ( $i = 0; $i < $batch_size; $i++ ) {
// Retrieve Counter
$counter = get_option('iaff_bulk_updater_counter');
$counter = intval ($counter);
global $wpdb;
$image = $wpdb->get_row("SELECT ID, post_parent FROM {$wpdb->prefix}posts WHERE post_type='attachment' AND post_mime_type LIKE 'image%' ORDER BY post_date LIMIT 1 OFFSET {$counter}");
// Die if no image
if ($image === NULL) {
return;
}
// Running the pro module
iaffpro_auto_image_attributes_pro( $image, true );
// Increment counter and update it
$counter++;
update_option( 'iaff_bulk_updater_counter', $counter );
}
/**
* 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');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment