Categories
How To

Image Attribution

How to add more fields to your images in WordPress.

Table of Contents

Did you know you could add fields to the media uploader?

In my case, I was at a convention and a fellow reporter (welcome to my new weird life) muttered she wished it was easier to have just a photo ‘credit’ line when she uploaded media from networks. I asked what system she used to run her site and when she said WordPress, I gave her my biggest smile.

Image Filters Image Filters

There are two things we need to filter here.

  1. Add our attribution field to the editor
  2. Save the attribution data

That’s it. WordPress handles the rest.

add_filter( 'attachment_fields_to_edit', 'halfelf_add_attachment_attribution', 10000, 2);
add_action( 'edit_attachment', 'halfelf_save_attachment_attribution' );

function halfelf_add_attachment_attribution( $form_fields, $post ) {
	$field_value = get_post_meta( $post->ID, 'HALFELF_attribution', true );
	$form_fields[ 'HALFELF_attribution' ] = array(
		'value'    => $field_value ? $field_value : '',
		'label'    => __( 'Attribution' ),
		'helps'    => __( 'Insert image attribution here (i.e. "NBCUniversal" etc)' )
	);
	return $form_fields;
}

function halfelf_save_attachment_attribution( $attachment_id ) {
	if ( isset( $_REQUEST['attachments'][$attachment_id]['lwtv_attribution'] ) ) {
		$attribution = $_REQUEST['attachments'][$attachment_id]['HALFELF_attribution'];
		update_post_meta( $attachment_id, 'HALFELF_attribution', $attribution );
	}
}

Top ↑

End Result? End Result?

It shows up a little lower down than I’d like (I’d prefer it to be up where the URL is) but it works:

An example of Image Attribution

Oh and yes, I emailed her the code as a stand-alone plugin. Her IT person was thrilled.