Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ciccarone/db3985a34e95fff72118c01478b67088 to your computer and use it in GitHub Desktop.
Save ciccarone/db3985a34e95fff72118c01478b67088 to your computer and use it in GitHub Desktop.
Automatically add ALT tags to images uploaded to Wordpress media library based off of the file name.
// WordPress function to automatically
// add ALT tags to images on upload.
function alt_tag_adder( $post_ID ) {
if ( wp_attachment_is_image( $post_ID ) ) {
$tc_title = get_post( $post_ID )->post_title;
$tc_title = preg_replace( '%\s*[-_\s]+\s*%', ' ', $tc_title );
$tc_title = ucwords( strtolower( $tc_title ) );
$tc_meta = array(
'ID' => $post_ID,
);
update_post_meta( $post_ID, '_wp_attachment_image_alt', $tc_title );
wp_update_post( $tc_meta );
}
}
add_action( 'add_attachment', 'alt_tag_adder' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment