Add custom media field

Paste in functions.php

Field displaying in Edit media

/* custom media fild */
add_filter( ‘attachment_fields_to_edit’,  ‘attachment_fields_edit_hfp’, null, 2 );
add_filter( ‘attachment_fields_to_save’,  ‘attachment_fields_save_hfp’, null, 2 );
add_action( ‘admin_init’, ‘hfp_columns’);

function attachment_fields_edit_hfp( $form_fields, $post ) {

$hfp_prevention = (bool) get_post_meta($post->ID, ‘_myhfp_prevention’, true);
$checked = ($hfp_prevention) ? ‘checked’ : ”;
$form_fields[‘hfp’] = array(
‘label’ => ”,
‘input’ => ‘html’,
‘html’ => “<label><strong>Show for visitors</strong></label><br/><input type=’checkbox’ {$checked} name=’attachments[{$post->ID}][hfp_prevention]’ id=’attachments[{$post->ID}][hfp_prevention]’ />Show for visitors?”,
‘value’ => $hfp_prevention,
‘helps’ => ”
);
return $form_fields;

}
function attachment_fields_save_hfp($post, $attachment) {

if (isset($attachment[‘hfp_prevention’]))
$hfp_prevention = ($attachment[‘hfp_prevention’] == ‘on’) ? 1 : 0;
else $hfp_prevention=0;
update_post_meta($post[‘ID’], ‘_myhfp_prevention’, $hfp_prevention);

return $post;

}

function hfp_column( $cols ) {

$cols[“hfp”] = “Show for visitors”;
return $cols;

}

function hfp_column_value( $column_name, $id ) {

$meta = wp_get_attachment_metadata($id);
$hfp=get_post_meta($id, ‘_myhfp_prevention’, true);
if ($hfp==1) echo “Yes”;

}

function hfp_column_sortable( $cols ) {

$cols[“hfp”] = “hfp”;
return $cols;

}
function hfp_columns() {

add_filter( ‘manage_media_columns’, ‘hfp_column’ );
add_action( ‘manage_media_custom_column’, ‘hfp_column_value’, 10, 2 );
add_filter( ‘manage_upload_sortable_columns’, ‘hfp_column_sortable’ );

}

Advertisements

Tagged:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: