0

It seems that Wordpress use the exif title to fill the caption field, but I need it to use the exif copyright data instead to give automaticaly credit to photographers. What code should I use, please help me because I can't find clear examples of that action.

<?php

add_action( 'add_attachment', 'my_set_image_meta_upon_image_upload' );

function my_set_image_meta_upon_image_upload( $post_ID ) {


    if ( wp_attachment_is_image( $post_ID ) ) {

        $my_image_title = get_post( $post_ID )->post_title;

        // Both give illegal string offset 'image_meta' 
        $meta_data = wp_read_image_metadata( $post_ID )['image_meta'];
        $meta_data = wp_get_attachment_metadata( $post_ID )['image_meta'];

        $my_image_meta = array(
            'ID'        => $post_ID,            
            'post_excerpt'  => $meta_data['copyright'],     
            'post_content'  => $meta_data['copyright'],     
        );


        update_post_meta( $post_ID, '_wp_attachment_image_alt', $meta_data );


        wp_update_post( $my_image_meta );

    } 
}
  • You need another hook. The problem is explained here ([wp_get_attachment_metadata returns false with add_action() 'add_attachment' hook](https://wordpress.stackexchange.com/q/61519/12615)) but I don't what's the best hook for you. – brasofilo May 26 '18 at 22:46

1 Answers1

0
function filter_wp_generate_attachment_copyright( $metadata, $attachment_id ) { 

    $cpr = $metadata[image_meta][copyright];

    $my_image_meta = array(
        'ID'        => $attachment_id,          // Specify the image (ID) to be updated
        'post_excerpt'  => 'Photo © ' . $cpr,       // Set image Caption (Excerpt) to copyright
    );  

    wp_update_post( $my_image_meta );
    return $metadata; 
}; 
add_filter( 'wp_generate_attachment_metadata', 'filter_wp_generate_attachment_copyright', 10, 2 );