Last active
May 26, 2018 04:57
-
-
Save clreed87/2b12e871dec61b2c33fc064ccbb1aa39 to your computer and use it in GitHub Desktop.
Populate image title, alt-text, caption, and description on upload
This file contains 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 | |
// Do not include the opening php tag. | |
// Populate image title, alt-text, caption, and description on upload | |
add_action( 'add_attachment', 'crt_set_image_meta' ); | |
function crt_set_image_meta( $post_ID ) { | |
// Check if uploaded file is an image, else do nothing | |
if ( wp_attachment_is_image( $post_ID ) ) { | |
// Set image meta to 'Status: Date' | |
$timezone = get_option('timezone_string'); | |
date_default_timezone_set( $timezone ); | |
$image_title = 'Status: '.date( 'Y-m-d H.i.s' ); | |
$image_meta = array( | |
'ID' => $post_ID, | |
'post_title' => $image_title, | |
'post_excerpt' => $image_title, | |
'post_content' => $image_title, | |
); | |
// Set the image alt-text | |
update_post_meta( $post_ID, '_wp_attachment_image_alt', $image_title ); | |
// Set the image meta | |
wp_update_post( $image_meta ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment