Created
February 10, 2020 10:39
-
-
Save akshuvo/4fffeb7578b704660bcdf858810cc867 to your computer and use it in GitHub Desktop.
Add a Copyright Field to the Media Uploader in WordPress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * 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 $id | |
| * | |
| * @return array | |
| * @usages [show_img_with_copyright id=63] | |
| */ | |
| 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' ); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usages
[show_img_with_copyright id=63]