【问题标题】:WordPress - Hidden custom meta field for imagesWordPress - 隐藏的图像自定义元字段
【发布时间】:2012-10-09 03:47:23
【问题描述】:

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

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

感谢您的帮助。

【问题讨论】:

    标签: wordpress media custom-fields


    【解决方案1】:

    这里得到了回答: 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-06
      • 1970-01-01
      相关资源
      最近更新 更多