最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I insert and display copyright owner field on featured-images, pictures and galleries in articles?

programmeradmin0浏览0评论

My request is to provide a separate copyright field for images and to display the copyright information for each image. For this purpose I have inserted a separate field in the media library based on this source (/) Unfortunately, this snippet is only available for featured-images.

How can I use this snippet for images and galleries within articles? Would that work with the codebase? If not, how would you solve this problem without plugins? I would be very grateful for your help.

My request is to provide a separate copyright field for images and to display the copyright information for each image. For this purpose I have inserted a separate field in the media library based on this source (https://bavotasan/2012/add-a-copyright-field-to-the-media-uploader-in-wordpress/) Unfortunately, this snippet is only available for featured-images.

How can I use this snippet for images and galleries within articles? Would that work with the codebase? If not, how would you solve this problem without plugins? I would be very grateful for your help.

Share Improve this question asked Feb 10, 2020 at 9:39 sofarockersofarocker 1
Add a comment  | 

1 Answer 1

Reset to default 0

Try this:

/**
 * Adding a "Copyright" field to the media uploader $form_fields array
 *
 * @param array $form_fields
 * @param object $post
 *
 * @return array
 */
function add_copyright_field_to_media_uploader( $form_fields, $post ) {
    $form_fields['copyright_field'] = array(
        'label' => __('Copyright'),
        'value' => get_post_meta( $post->ID, '_custom_copyright', true ),
        'helps' => 'Set a copyright credit for the attachment'
    );

    return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'add_copyright_field_to_media_uploader', null, 2 );

/**
 * Save our new "Copyright" field
 *
 * @param object $post
 * @param object $attachment
 *
 * @return array
 */
function add_copyright_field_to_media_uploader_save( $post, $attachment ) {
    if ( ! empty( $attachment['copyright_field'] ) ) 
        update_post_meta( $post['ID'], '_custom_copyright', $attachment['copyright_field'] );
    else
        delete_post_meta( $post['ID'], '_custom_copyright' );

    return $post;
}
add_filter( 'attachment_fields_to_save', 'add_copyright_field_to_media_uploader_save', null, 2 );

/**
 * Display our new "Copyright" field
 *
 * @param int $attachment_id
 *
 * @return array
 */
function get_featured_image_copyright( $atts ) {
    extract(shortcode_atts(array(
        'id' => '',
    ), $atts));

    $attachment_id = ( empty( $id ) ) ? get_post_thumbnail_id() : (int) $id;

    if ( $attachment_id ){
        $output = "<div class='img-wrapper'>";       
         $output .= "<img src='".wp_get_attachment_url($attachment_id)."'>";
         $output .= "<span>".get_post_meta( $attachment_id, '_custom_copyright', true )."</span>";
        $output .= "</div>";
        return $output;
    }

}
add_shortcode( 'show_img_with_copyright', 'get_featured_image_copyright' );

Then use Shortcode with attachment id anywhere in the articles like this:

[show_img_with_copyright id=63]

Tested and worked

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论