facelordgists
4/18/2013 - 5:36 AM

PHP: Wordpress function to add a field to the media uploader

PHP: Wordpress function to add a field to the media uploader

/**
 * Add URL link field to media uploader
 */

function slide_attachment_field_link( $form_fields, $post ) {

	$form_fields['rsmm-photo-url'] = array(
		'label' => 'Slide URL',
		'input' => 'text',
		'value' => get_post_meta( $post->ID, 'slide_url', true ),
		'helps' => 'Add URL that this slide will link to, if used in a gallery',
	);

	return $form_fields;
}

add_filter( 'attachment_fields_to_edit', 'slide_attachment_field_link', 10, 2 );

/**
 * Save values in media uploader
 */

function slide_attachment_field_credit_save( $post, $attachment ) {

	if( isset( $attachment['rsmm-photo-url'] ) )
update_post_meta( $post['ID'], 'slide_url', esc_url( $attachment['rsmm-photo-url'] ) );

	return $post;
}

add_filter( 'attachment_fields_to_save', 'slide_attachment_field_credit_save', 10, 2 );
?>