Skip to content

Instantly share code, notes, and snippets.

@leavlzi
Created October 6, 2017 18:37
Show Gist options
  • Save leavlzi/dd1fc3107733c146dfc58301a71533a5 to your computer and use it in GitHub Desktop.
Save leavlzi/dd1fc3107733c146dfc58301a71533a5 to your computer and use it in GitHub Desktop.
Automatically add Alt Tag to WordPress Image Uploads
<?php
/*
Automatically add Alt Tag to WordPress Image Uploads
/* ------------------------------------ */
add_action( 'add_attachment', 'ced_add_image_meta_data' );
function ced_add_image_meta_data( $attachment_ID ) {
$filename = $_REQUEST['name'];
$withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $filename);
$withoutExt = str_replace(array('\'','_'), ' ', $withoutExt);
$my_post = array(
'ID' => $attachment_ID,
);
wp_update_post( $my_post );
update_post_meta($attachment_ID, '_wp_attachment_image_alt', $withoutExt );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment