Skip to content

Instantly share code, notes, and snippets.

@clreed87
Last active May 26, 2018 04:57
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 clreed87/2b12e871dec61b2c33fc064ccbb1aa39 to your computer and use it in GitHub Desktop.
Save clreed87/2b12e871dec61b2c33fc064ccbb1aa39 to your computer and use it in GitHub Desktop.
Populate image title, alt-text, caption, and description on upload
<?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