iaffpro_get_custom_attribute_tag_{%tagname%}()

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.

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