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();