3

When uploading an image, Wordpress reads the description meta data from the jpg via wp_read_image_metadata and inserts it into the post_content field when adding the attachment via media_handle_upload or media_handle_sideload

But when adding a image to the editor in Wordpress, it looks for the caption in the post_excerpt field via image_media_send_to_editor

What I'd like is just some continuity, in that when our images are uploaded, the field populated by WordPress for the images meta description is the one used for the caption when placing an image in the editor.

Can anyone suggest a way I can hook into any of these functions in order to match up the process? I don't really care which way I fix it, although I tend to think that post_excerpt leads to less database overhead since it's a 'smaller' field.

Any help is greatly appreciated.

Thanks.

1

1 Answer 1

1

I wonder if this will work for you:

add_action( 'add_attachment', function( $attachment_id ){
    $a = get_post( $attachment_id );
    if ( is_object( $a ) && 'image' === substr( $a->post_mime_type, 0, 5 ) )
        wp_insert_attachment( array( 'ID' => $a->ID, 'post_excerpt' => $a->post_content ) );

});

or with less queries:

add_action( 'add_attachment', function( $attachment_id ){
    global $wpdb;
    if( ! empty( $attachment_id ) )
        $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts 
                                       SET post_excerpt = post_content 
                                       WHERE ID = %d LIMIT 1", $attachment_id ) );

});

where the description (post_content) is copied to the caption (post_excerpt) when the add_attachment is fired just after the attachment has been inserted (added).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.