Skip to content

Instantly share code, notes, and snippets.

@murtazakarimi
Last active July 23, 2021 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save murtazakarimi/4c97908162ee0851ea7cfcc4547e6be6 to your computer and use it in GitHub Desktop.
Save murtazakarimi/4c97908162ee0851ea7cfcc4547e6be6 to your computer and use it in GitHub Desktop.
wp-update-image-alt-tags-on-upload-from-filename
<?php
add_action( 'add_attachment', 'update_image_alt_tags_on_upload' );
function update_image_alt_tags_on_upload( $post_ID ) {
if ( wp_attachment_is_image( $post_ID ) ){
$image_name = get_post( $post_ID )->post_title;
$image_name = preg_replace( '%\s*[-_\s]+\s*%', ' ', $image_name ); // Replace '-''_' with single space
$image_name = ucwords( strtolower( $image_name ) ); // Capitalize first letter
update_post_meta( $post_ID, '_wp_attachment_image_alt', $image_name );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment