Wordpress上传jpg应该写exif版权标题

时间:2018-05-25 15:50:18

标签: wordpress exif copyright-display

似乎Wordpress使用exif标题来填充标题字段,但我需要使用exif版权数据来自动给予摄影师信用。 我应该使用什么代码,请帮助我,因为我无法找到该操作的明确示例。

<?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 );

    } 
}

1 个答案:

答案 0 :(得分: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 );