WordPress media upload define default meta values

i managed to automatically fill in a default caption upon upload

add_action( 'add_attachment', 'as_set_image_meta_upon_image_upload' );
function as_set_image_meta_upon_image_upload( $post_ID ) {
// Check if uploaded file is an image, else do nothing
    if ( wp_attachment_is_image( $post_ID )) {
        //default photographer
        if (strlen(trim(get_post_meta( $post_ID, 'photographer', true))) == 0 ) {
            update_post_meta( $post_ID, 'photographer', 'unknown' );
        }
        //default copyright
        if (strlen(trim(get_post_meta( $post_ID, 'copyright', true))) == 0 ) {
            update_post_meta( $post_ID, 'copyright', 'not defined' );
        }
        //default caption
        $my_image_meta = array(
            'ID' => $post_ID,
            'post_excerpt' => '[Attribution: unknown; Copyright: not defined] ',
        );
        wp_update_post( $my_image_meta );   
    }
}

when i tried to load the actual values of ‘photographer’ and ‘copyright’ they returned empty, i suppose because there are just being filled in the same hook. good enough though

Leave a Comment