Rename Image Alt Text or Image Title Automatically in Bulk

Let’s assume that you have a need to rename (or replace) a specific phrase or word in all your image attributes across your website. Image Attributes Pro can help!

For example, you had appended your business name along with all your image filenames. You had used this filename to generate image attributes. Now you rebranded and you wish to change the attributes sitewide with the new business name.

Please note: Image Attributes Pro does not rename the actual images yet (as of version 1.3). This doc is about renaming the image attributes and not the filename.

To do so, we will take advantage of the iaffpro_image_attributes WordPress filter in the pro add-on. If code isn’t your think, don’t panic and read on, there is a easy to use code snippet with clear instructions below.

Example Snippet

For this example, we have an image that is title old-small-business.jpg. Image Attributes are generated from the image filename and will all contain Old Small Business in it.

You have rebranded to your new business name, New Flashy Business. Here is the code sample that will replace the the old phrase with the new.

Add this to the functions.php of your active theme and then run the Bulk Updater on all images.

Important! Please remember to take a full database backup and run a test update (using the Test Bulk Updater button) before doing this. Changes once made cannot be automatically undone without a database backup.

/**
 * 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_modify_image_attributes( $attributes, $image_id, $parent_post_id ) {
	
	foreach ( $attributes as $key => $attribute ) {
		
		$attributes[$key] = str_ireplace( 'Old Small Business', 'New Flashy Business', $attribute );
	}
	
	return $attributes;
}
add_filter( 'iaffpro_image_attributes', 'prefix_iap_modify_image_attributes', 10, 3 );

Please note:

  • The whole process does not replace existing attributes as such. New attributes are generated from the filename or post title (based on your options in the plugin settings) and then if the generated attributes contains the old phrase (Old Small Business in this example), it will be replaced.
  • If you have manually added phrases that were not auto generated, then this replacement does not replace that. For example, if you had manually edited the Image Title of some images before, then running the Bulk Updater will not replace them.
  • The above example does a case insensitive replacement. If you want case sensitive replacement, use str_replace instead of str_ireplace in the example code above.

Got questions? Please do not hesitate to get in touch.

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