WordPress - 为图像隐藏自定义元字段

问题描述:

我试图在WordPress中向媒体上传程序添加自定义字段。我一直在努力,但我想让自定义字段的关键字隐藏起来。WordPress - 为图像隐藏自定义元字段

如果您熟悉WordPress处理自定义字段的方式,您会知道将键设置为“_something”会将该键从用户可见的下拉列表中隐藏起来。

/** 
* Add Video URL fields to media uploader 
* 
* http://www.billerickson.net/wordpress-add-custom-fields-media-gallery/ 
* 
* @param $form_fields array, fields to include in attachment form 
* @param $post object, attachment record in database 
* @return $form_fields, modified form fields 
*/ 

    function capgun2012_attachment_fields($form_fields, $post) { 

     $form_fields['capgun2012_video_url'] = array(
      'label' => 'Vimeo URL', 
      'input' => 'text', 
      'value' => get_post_meta($post->ID, 'capgun2012_video_url', true), 
      'helps' => 'If provided, photo will be displayed as a video', 
     ); 

     return $form_fields; 
    } 

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

/** 
* Save values of Photographer Name and URL in media uploader 
* 
* @param $post array, the post data for database 
* @param $attachment array, attachment fields from $_POST form 
* @return $post array, modified post data 
*/ 

    function capgun2012_attachment_fields_save($post, $attachment) { 

     if(isset($attachment['capgun2012_video_url'])) { 
      update_post_meta($post['ID'], 'capgun2012_video_url', $attachment['capgun2012_video_url']); 
     } 

     return $post; 
    } 

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

如果我只需更换“capgun2012_video_url”与“_capgun2012_video_url”的所有事件,然后它不工作。我开始认为媒体上传器与隐藏的自定义字段不兼容。

请参阅附件截图我不想发生(在自定义字段中显示的自定义键下拉)。

enter image description here

感谢您的帮助。

这是在这里找到答案: http://devilsworkshop.org/adding-custom-fields-to-wordpress-media-gallery-upload/

设置字段:

/* For adding custom field to gallery popup */ 
function rt_image_attachment_fields_to_edit($form_fields, $post) { 
    // $form_fields is a an array of fields to include in the attachment form 
    // $post is nothing but attachment record in the database 
    //  $post->post_type == 'attachment' 
    // attachments are considered as posts in WordPress. So value of post_type in wp_posts table will be attachment 
    // now add our custom field to the $form_fields array 
    // input type="text" name/id="attachments[$attachment->ID][custom1]" 
    $form_fields["rt-image-link"] = array(
     "label" => __("Custom Link"), 
     "input" => "text", // this is default if "input" is omitted 
     "value" => get_post_meta($post->ID, "_rt-image-link", true), 
       "helps" => __("To be used with special slider added via [rt_carousel] shortcode."), 
    ); 
    return $form_fields; 
} 
// now attach our function to the hook 
add_filter("attachment_fields_to_edit", "rt_image_attachment_fields_to_edit", null, 2); 

而这款以保存:

function rt_image_attachment_fields_to_save($post, $attachment) { 
    // $attachment part of the form $_POST ($_POST[attachments][postID]) 
     // $post['post_type'] == 'attachment' 
    if(isset($attachment['rt-image-link'])){ 
     // update_post_meta(postID, meta_key, meta_value); 
     update_post_meta($post['ID'], '_rt-image-link', $attachment['rt-image-link']); 
    } 
    return $post; 
} 
// now attach our function to the hook. 
add_filter("attachment_fields_to_save", "rt_image_attachment_fields_to_save", null , 2);