Alter media caption/description conflict in WordPress?

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).

Leave a Comment