我正在尝试将自定义字段添加到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”,那么它就不起作用了。我开始认为媒体上传器在隐藏的自定义字段中效果不佳。
请参阅我不想发生的附加屏幕截图(自定义字段中显示的自定义键下拉列表)。
感谢您的帮助。
答案 0 :(得分:0)
这里的答案如下: 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);