Functions – Image Attributes Pro https://imageattributespro.com Bulk Update WordPress Image Attributes Sun, 09 Apr 2023 10:03:23 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.3 iaffpro_bu_run_bulk_updater() https://imageattributespro.com/codex/iaffpro_bu_run_bulk_updater/ https://imageattributespro.com/codex/iaffpro_bu_run_bulk_updater/#respond Mon, 27 Mar 2023 18:08:26 +0000 https://imageattributespro.com/?post_type=codex&p=3499 Runs the bulk updater of Image Attributes Pro, one batch at a time. Image attributes will be updated as per the Bulk Updater Settings.

iaffpro_bu_run_bulk_updater( $batch_size );

Accepts one parameter, $batch_size which is an integer. The default batch size is 20 which can be filtered using iaffpro_bu_batch_size filter.

Image Attributes Pro version 4.2 and later varies the images per batch based on the server conditions. However, if you override the default batch size using iaffpro_bu_batch_size filter, the variable batch size is not used.

Example Usage

// Run Image Attributes Pro Bulk Updater
iaffpro_bu_run_bulk_updater( 25 );

For a better more elaborate example, refer: Run Image Attributes Pro Bulk Updater Programmatically / via Cron Job

]]>
https://imageattributespro.com/codex/iaffpro_bu_run_bulk_updater/feed/ 0
iaffpro_update_attributes_in_post_by_post_id() https://imageattributespro.com/codex/iaffpro_update_attributes_in_post_by_post_id/ https://imageattributespro.com/codex/iaffpro_update_attributes_in_post_by_post_id/#respond Tue, 22 Mar 2022 05:08:08 +0000 https://imageattributespro.com/?post_type=codex&p=2575 Update image attributes of all images in a given post. Post can be specified by passing post ID or post object.

Image attributes will be updated as per the Bulk Updater Settings of Image Attributes Pro.

iaffpro_update_attributes_in_post_by_post_id( $post_id );
  • Requires Image Attributes Pro 3.0 and above.
  • Accepts the post ID ($post_id) as a parameter.
  • Does not return anything.
  • Can be used for posts, pages, WooCommerce products and any custom post type.

Example Usages

// Update image attributes of all images for a post with ID 247
iaffpro_update_attributes_in_post_by_post_id( 247 );
/**
 * Update image attributes as per the Bulk Updater Settings of Image Attributes Pro 
 * when a post is saved or updated. 
 * 
 * @author Arun Basil Lal
 * @link https://imageattributespro.com/codex/iaffpro_update_attributes_in_post_by_post_id/
 */
function prefix_iap_update_image_attributes_on_save_post( $post_id ) {

	if ( ! function_exists( 'iaffpro_update_attributes_in_post_by_post_id' ) ) {
		return;
	}

	// post_id from save_post will be the ID of the revision. Finding parent post_id.
	if ( $post_parent_id = wp_get_post_parent_id( $post_id ) ) {
		$post_id = $post_parent_id;
	}

	/**
	 * iaffpro_update_attributes_in_post_by_post_id() will call save_post again.
	 * To prevent this, unhook action right before. 
	 * 
	 * @link https://developer.wordpress.org/reference/hooks/save_post/#avoiding-infinite-loops
	 */
	remove_action( 'save_post', 'prefix_iap_update_image_attributes_on_save_post' );
    
	iaffpro_update_attributes_in_post_by_post_id( $post_id );
}
add_action( 'save_post', 'prefix_iap_update_image_attributes_on_save_post' );
]]>
https://imageattributespro.com/codex/iaffpro_update_attributes_in_post_by_post_id/feed/ 0
iaffpro_get_custom_attribute_tag_{%tagname%}() https://imageattributespro.com/codex/iaffpro_get_custom_attribute_tag_tagname/ https://imageattributespro.com/codex/iaffpro_get_custom_attribute_tag_tagname/#comments Sun, 04 Jul 2021 16:23:21 +0000 https://imageattributespro.com/?post_type=codex&p=2100 Image Attributes Pro version 2.0 introduced custom tags that can be used to build your own custom attributes.

For example, if you want to set your image alt text as Post Title followed by Rank Math Focus Keyword, you can use %posttitle% - %rankmathfocuskw% as the custom attribute.

Image Alt Text As Post Title Rank Math Focus Keyword

Image Attributes Pro will replace %posttitle% with the title of the post to which the image is uploaded to and %rankmathfocuskw% with the focus keyword set in Rank Math plugin.

You can easily extend beyond the available tags. Let’s see that with an example.

Example: Create a tag for the name of your business

Let’s say you wish to add the name of your business as part of some of your attributes. Let’s call this tag %business_name%

Custom Attribute Tag Business_name

Now for Image Attributes Pro to recognise this tag, you have to define a function with the name iaffpro_get_custom_attribute_tag_{%tagname%} where {%tagname%} is the name of your custom tag.

So back to our example, your function is to be named iaffpro_get_custom_attribute_tag_business_name. It would look something like this.

/**
 * Custom tag %business_name% for Image Attributes Pro
 * 
 * @param $image_id (integer) The ID of the image that is being updated. 
 * @param $parent_post_id (integer) Post to which the image is attached (uploaded) to. 
 * @param $args (array) An array containing additional arguments.
 * 
 * @return (string) Name of the business. 
 * 
 * @refer https://imageattributespro.com/codex/iaffpro_get_custom_attribute_tag_tagname/
 */
function iaffpro_get_custom_attribute_tag_business_name( $image_id, $parent_post_id, $args = array() ) {
	return 'My Dope Business Name';
}

Add this in the functions.php of your active theme or define it in a plugin and Image Attributes Pro will replace %business_name% with My Dope Business Name while generating the image attributes.

Things To Note

  • The function must strictly be in this format: iaffpro_get_custom_attribute_tag_{%tagname%} where {%tagname%} is replaced by the name of your custom tag.
  • The function must return a string.
  • The function will have access to three parameters. $image_id, $parent_post_id, and $args.
  • $image_id (integer) is the ID of the image that is being updated.
  • $parent_post_id (integer) The post to which the image is attached (uploaded) to. 0 if the image is not attached to any post.
  • The third parameter changed to $args in Image Attributes Pro version 4.3. $bulk (boolean) will be true when called from Bulk Updater. For new image uploads, it will be false.

$args (array) includes array keys:

  • bulk (boolean) True when called from Bulk Updater. False by default.
  • image_url (string) Optionally pass image url. Used when generating attributes for external images.
  • attribute (string) Attribute setting requested prefixed with $bu_prefix ( bu_prefix will be bu_ when called from bulk updater and empty string otherwise).

Refer to \auto-image-attributes-pro\functions\custom-attributes.php for more examples.

]]>
https://imageattributespro.com/codex/iaffpro_get_custom_attribute_tag_tagname/feed/ 1