iaffpro_image_name_from_filename_pre

Filter the file name of the image before Image Attributes Pro starts processing it. Useful to manually intervene each filename before custom filters and regex filters act on it.

/**
* Filter the name of the image before Image Attributes Pro starts processing.
* 
* @since 4.8
* 
* @param $image_name (string) Name of the image.
* @param $image_id (int) ID of the image to work on. 0 for external images.
* @param $bulk (boolean) True when called from the Bulk Updater. False otherwise.
* @param $image_url (string) Optionally pass image url. Used only when generating attributes for external images.
*/
$image_name = apply_filters( 'iaffpro_image_name_from_filename_pre', $image_name, $image_id, $bulk, $image_url );

Example Usage

This filter can be used in conjunction with the iaffpro_image_attributes filter to preserve or bypass Image Attributes Pro’s processing.

For example, let’s say you want to make sure the word, WordPress is represented as is regardless of what capitalization is applied to the rest of the filename.

The following code will first replace the keyword (WordPress) with a placeholder (IAPWPPLACEHOLDER).

/**
 * Preserve the word 'WordPress' while generating image attributes.
 * 
 * Replace 'WordPress' with a placeholder (IAPWPPLACEHOLDER) in the image filename. 
 * This can later be replaced in the iaffpro_image_attributes filter.
 * 
 * @param $image_name (string) Name of the image.
 * @param $image_id (int) ID of the image to work on. 0 for external images.
 * @param $bulk (boolean) True when called from the Bulk Updater. False otherwise.
 * @param $image_url (string) Optionally pass image url. Used only when generating attributes for external images.
 * 
 * @return $image_name (string) Name of the image after adding the placeholder.
 * 
 * @author Arun Basil Lal
 * @link https://imageattributespro.com/codex/iaffpro_image_name_from_filename_pre/
 */
function prefix_iap_preserve_keyword_in_filename( $image_name, $image_id, $bulk, $image_url ) {
	return str_replace( 'WordPress', 'IAPWPPLACEHOLDER', $image_name ); // Adds a placeholder.
}
add_filter( 'iaffpro_image_name_from_filename_pre', 'prefix_iap_preserve_keyword_in_filename', 10, 4 );

Then later the placeholder can be replaced back to the keyword after all the processing is done.

/**
 * Modify image attributes generated by Image Attributes Pro
 *
 * @param $attributes (array) Associative array of image attributes.
 * @param $image_id (int) ID of the current image.
 * @param $parent_post_id (int) ID of the post the image is inserted into. 0 for images not attached to a post.
 * 
 * @author Arun Basil Lal
 * @link https://imageattributespro.com/codex/iaffpro_image_attributes/
 */
function prefix_iap_restore_keyword_in_filename( $attributes, $image_id, $parent_post_id ) {

	foreach ( $attributes as $key => $value ) {
		$attributes[$key] = str_replace( 'IAPWPPLACEHOLDER', 'WordPress', $attributes[$key] );
	}

	return $attributes;
}
add_filter( 'iaffpro_image_attributes', 'prefix_iap_restore_keyword_in_filename', 10, 3 );
Was this article helpful?
Yes, thanks! 👍Not really 👎