0

我正在尝试向 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”,那么它就行不通了。我开始认为媒体上传器不能很好地处理隐藏的自定义字段。

请参阅我不希望发生的附加屏幕截图(自定义字段下拉菜单中显示的自定义键)。

在此处输入图像描述

谢谢您的帮助。

4

1 回答 1

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);
于 2012-10-08T19:47:23.213 回答