Created
October 6, 2017 18:37
-
-
Save leavlzi/dd1fc3107733c146dfc58301a71533a5 to your computer and use it in GitHub Desktop.
Automatically add Alt Tag to WordPress Image Uploads
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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