Add a Copyright Field to the Media Uploader in WordPress
by c.bavota | Posted in Tutorials | 13 comments
When you upload something into WordPress using the Media Uploader, you can include information such as a caption or a description. If you wanted to include something else, you would first have to create a custom field by hooking into the attachment_fields_to_edit filter. I decided to add a “Copyright” field so that I could credit each item I upload to the original copyright owner and displayed on the front end wherever I want.
Here is the code you need to add to your functions.php file:
/**
* 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( $attachment_id = null ) {
$attachment_id = ( empty( $attachment_id ) ) ? get_post_thumbnail_id() : (int) $attachment_id;
if ( $attachment_id )
return get_post_meta( $attachment_id, '_custom_copyright', true );
}
With the above code in place, you can now use the following snippet to display your attachment’s new “Copyright” field within one of your page templates.
<?php echo get_featured_image_copyright(); ?>
If you leave the attachment ID parameter blank, it will display the featured image “Copyright” if your post has a featured image set.



Nice trick, man! Have used wordpress for a while, never released this.
yes ! and it can be do easier than ever, i think
Wow! this is very useful. I currently do it through the caption field.
Cool tip. Bill Erickson has a very similar tutorial on adding fields for images (with good comment discussion): http://www.billerickson.net/wordpress-add-custom-fields-media-gallery/
The frustration with both is that it only works with featured images and of no value to inserted images… (good discussion in Bill’s comment on that)
You could do this working with the
send_to_editorJS call. I haven’t had a chance to play with it but I’ll look into it.Is there really no easy way to make this specific to each image rather than just the featured image? I thought that was the point of using attachment_id?
Two errors in your code:
should be:
and
is missing a semi-colon at the end.
Great tip! Thanks
Awesome. Thanks for pointing out the errors.
Hi there are using WordPress for your site platform?
I’m new to the blog world but I’m trying to get started and create my own. Do you need any html coding knowledge to make your own blog? Any help would be really appreciated!
Not at all. You can easily sign up for a blog at WordPress.com or install WP on your own server. It’s pretty easy to do. Read about it here: http://codex.wordpress.org/Installing_WordPress
Pretty! This was a really wonderful article. Thanks
for supplying this info.
Have you ever thought about publishing an ebook or guest authoring on other websites?
I have a blog based on the same subjects you discuss and would really like to have you share
some stories/information. I know my visitors would enjoy
your work. If you are even remotely interested, feel free to send me an email.
Woah! I’m really enjoying the template/theme of this site. It’s simple, yet effective.
A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done
a superb job with this. In addition, the blog loads super quick
for me on Chrome. Excellent Blog!