How Add New Custom Filed ON Media Image Pages : WordPress


Hello Friends!!

Hope you all are doing well 🙂

In admin if you want to add new custom field on media details page then you can do it in a easy way. You will not need to edit in wordpress core files , using wordpress hooks you can do it by add the code in your theme function file.

I am given to a example for add to new field “External URL”, You will need to add the code in your theme function file.

 

/** Add new Custom Field On Media detail page */
function add_custom_media_fields($form_fields, $post) {
    $form_fields["externalUrl"] = array(
        "label" => __("External URL"),
        "input" => "text", // this is default if "input" is omitted
        "value" => get_post_meta($post->ID, "externalUrl", true)
    );
     
    return $form_fields;
}
add_filter("attachment_fields_to_edit", "add_custom_media_fields", null, 2); 

/** Save the new custom field value into database*/
function save_custom_media_fields($post, $attachment) {
    $my_data = sanitize_text_field( $attachment['externalUrl'] );
    update_post_meta( $post['ID'], 'externalUrl', $my_data );
return $post;
}

add_filter('attachment_fields_to_save', 'save_custom_media_fields', 10, 2);

 
 
Firefox_Screenshot_2015-06-17T14-16-53.285Z
 
 
Hope you will all enjoy my code.

Advertisements

Sr. Software Engineer

Posted in Wordpress

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: