上传帖子图片时添加/更改MULPART_PARAMS参数

时间:2015-02-26 作者:Sandi Verdev

我正在开发一个插件,可以选择性地处理上传的内容(图像)。我设法在“将文件放在任何地方上载”文本下的“插入媒体”弹出窗口中添加了一些附加字段,但我需要将这些带有文件内容的参数发送到async-upload.php 上载时。

预WP v3。3、我一直在用这个来达到预期的效果:

jQuery(\'input:text[name="custom_text"]\').keyup(function() {
    wpUploaderInit.multipart_params.custom_text = jQuery(this).val(); 
});

1 个回复
SO网友:Dimetrodon

To send a customer data to the server, do the following

customFileFrame.uploader.options.uploader.params.yourCustomProperty = \'yourCustomValue\';
其中:

yourCustomProperty - parameter name
yourCustomValue - parameter value

processing data from the server

add_action(\'add_attachment\', array($this, \'addAttachmentParama\'));
<小时>
public function addAttachmentParama($post_id){
    if (isset($_REQUEST[\'yourCustomProperty\']) && isset($_REQUEST[\'action\']) && \'upload-attachment\' == $_REQUEST[\'action\']) {
        update_post_meta($post_id, \'yourCustomProperty\', $_REQUEST[\'yourCustomProperty\']);
     }
 }

Mini Plugin for example

<?php
/*
Plugin Name: ExsempleDimetrodonMediaUploader
Version: 0.0.1
Author: Dimetrodon
License: GPLv2 or later
*/

class ExsempleDimetrodonMediaUploader
{
    private static $instance;

    /**
     * Get (and instantiate, if necessary) the instance of the class
     *
     * @static
     * @return ExsempleDimetrodonMediaUploader
     */
    public static function get_instance(){
        if (!is_a(self::$instance, __CLASS__)) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    private function __clone(){}

    private function __construct(){
        add_shortcode(\'ExsempleDimetrodonMediaUploader\', array($this, \'MediaUploader\'));
        add_action(\'add_attachment\', array($this, \'addAttachmentParama\'));
    }

    public function addAttachmentParama($post_id){
        if (isset($_REQUEST[\'yourCustomProperty\']) && isset($_REQUEST[\'action\']) && \'upload-attachment\' == $_REQUEST[\'action\']) {
            update_post_meta($post_id, \'yourCustomProperty\', $_REQUEST[\'yourCustomProperty\']);
        }
    }

    public function MediaUploader()
    {
        wp_enqueue_media();
        ob_start();
        ?>
        <img id="image">
        <button class="image-uploader">Image Uploader</button>

        <script type="text/javascript">
            jQuery(function ($) {
                var customFileFrame;
                $(document).on(\'click\', \'.image-uploader\', function (event) {
                    if (typeof(customFileFrame) !== "undefined") {
                        customFileFrame.close();
                    }

                    //Create WP media frame.
                    customFileFrame = wp.media.frames.customHeader = wp.media({
                        //Title of media manager frame
                        title: "Select Image",
                        library: {
                            type: \'image\'
                        },
                        button: {
                            text: "Select Image"
                        },
                        multiple: false
                    });

                    //Sending of a custom parameters to the server
                    customFileFrame.uploader.options.uploader.params.yourCustomProperty = \'yourCustomValue\';

                    //callback for selected image
                    customFileFrame.on(\'select\', function () {
                        var attachment = customFileFrame.state().get(\'selection\').first().toJSON();
                        $(\'#image\').attr(\'src\', attachment.url);
                        //do something with attachment variable, for example attachment.filename
                        //Object:
                        //attachment.alt - image alt
                        //attachment.alt - image alt
                        //attachment.url
                        //attachment.author - author id
                        //attachment.caption
                        //attachment.dateFormatted - date of image uploaded
                        //attachment.description
                        //attachment.editLink - edit link of media
                        //attachment.filename
                        //attachment.height
                        //attachment.icon - don\'t know WTF?))
                        //attachment.id - id of attachment
                        //attachment.link - public link of attachment, for example ""http://example.com/?attachment_id=115""
                        //attachment.menuOrder
                        //attachment.mime - mime type, for example image/jpeg"
                        //attachment.name - name of attachment file, for example "my-image"
                        //attachment.status - usual is "inherit"
                        //attachment.subtype - "jpeg" if is "jpg"
                        //attachment.title
                        //attachment.type - "image"
                        //attachment.uploadedTo
                        //attachment.url - http url of image, for example "http://example.com/wp-content/uploads/2012/12/my-image.jpg"
                        //attachment.width
                    });

                    //Open modal
                    customFileFrame.open();
                    return false;
                });
            });
        </script>
        <?php
        return ob_get_clean();
    }
}
ExsempleDimetrodonMediaUploader::get_instance();

结束

相关推荐

使用MEDIA_HANDLE_UPLOAD上传多个文件

我有一个WordPress表单插件,我使用media_handle_upload 为了上传文件并直接获取ID并将其ID作为元日期附加到帖子中,我使用了以下方法:表单字段的HTML为:<input type=\"file\" name=\"my_file_upload\" id=\"my_file_upload\"> php代码是:$attach_id = media_handle_upload( \'my_file_upload\', $post_id ); if ( is_