Auto Add Image Title,Caption,Alt Text,Description while uploading Images in WordPress

added_post_meta seems like a good time to hook into a new image. Not only is the default meta already set but the function gives you the $post_id along with $meta_value which holds the attachment metadata. From there you can get all the fields and set the ones you want.

add_action('added_post_meta', 'wpse_20151219_after_post_meta', 10, 4);

function wpse_20151219_after_post_meta($meta_id, $post_id, $meta_key, $meta_value) {

    // _wp_attachment_metadata added
    if($meta_key === '_wp_attachment_metadata') {

        // ----------------------------------------------------------------------
        // POST
        // ----------------------------------------------------------------------

        // Change basic fields on attachment post
        wp_update_post(array(
                           'ID'           => $post_id,
                           'post_title'   => "This is a TITLE for $post_id",
                           'post_content' => "This is the DESCRIPTION for $post_id",
                           'post_excerpt' => "This is the CAPTION for $post_id",
                       ));

        // ----------------------------------------------------------------------
        // POST META
        // ----------------------------------------------------------------------

        // Change ALT Text
        update_post_meta($post_id, '_wp_attachment_image_alt', "This is the ALT Text for $post_id");

        // Add Custom Field
        update_post_meta($post_id, '_wpse_20121219_my_custom_meta', 'MyCustomMetaValue');

        // ----------------------------------------------------------------------
        // POST META ( ATTACHMENT METADATA )
        // ----------------------------------------------------------------------

        // Change Image Metadata
        $meta_value[ 'image_meta' ] = array_merge($meta_value[ 'image_meta' ], array(
            'credit'    => 'https://black-buddha.com',
            'camera'    => 'The Best Camera EVER!',
            'copyright' => date('Y'),
            'title'     => "This is a META TITLE for $post_id",
            'caption'   => "This is a META CAPTION for $post_id",
        ));

        // Update The Image Metadata
        wp_update_attachment_metadata($post_id, $meta_value);

        // _wp_attached_file
        // _wp_attachment_metadata (serialized)
        // _wp_attachment_image_alt
        // _wpse_20121219_my_custom_meta

        $attachment_meta = get_post_meta($post_id);

        // width
        // height
        // file
        // sizes
        // image_meta
        //      aperture
        //      credit
        //      camera
        //      caption
        //      created_timestamp
        //      copyright
        //      focal_length
        //      iso
        //      shutter_speed
        //      title
        //      orientation
        //      title
        //      keywords

        $attachment_metadata = wp_get_attachment_metadata($post_id);
    }
}

Leave a Comment