I'm writing a custom plugin and i would like to add the "Add Media" button.
I just need to upload media, not to retrieve any content/data from the uploaded file.
How can I add this button?
Thanks
I'm writing a custom plugin and i would like to add the "Add Media" button.
I just need to upload media, not to retrieve any content/data from the uploaded file.
How can I add this button?
Thanks
Share Improve this question asked Sep 2, 2013 at 15:09 PepozzoPepozzo 3672 gold badges6 silver badges11 bronze badges2 Answers
Reset to default 26If you want to add a add media button to your admin panels:
You need to use wp_enqueue_media();
add_action ( 'admin_enqueue_scripts', function () {
if (is_admin ())
wp_enqueue_media ();
} );
Then use this js:
jQuery(document).ready(function() {
var $ = jQuery;
if ($('.set_custom_images').length > 0) {
if ( typeof wp !== 'undefined' && wp.media && wp.media.editor) {
$('.set_custom_images').on('click', function(e) {
e.preventDefault();
var button = $(this);
var id = button.prev();
wp.media.editor.send.attachment = function(props, attachment) {
id.val(attachment.id);
};
wp.media.editor.open(button);
return false;
});
}
}
});
Use this html:
<p>
<input type="number" value="" class="regular-text process_custom_images" id="process_custom_images" name="" max="" min="1" step="1">
<button class="set_custom_images button">Set Image ID</button>
</p>
Show thumbnail preview instead of number
Just as a tweak, I did this...
changed the number input to hidden.
added:
$imgid =(isset( $instance[ 'imgid' ] )) ? $instance[ 'imgid' ] : "";
$img = wp_get_attachment_image_src($imgid, 'thumbnail');
And then... above the hidden field.
if($img != "") {
?>
<img src="<?= $img[0]; ?>" width="80px" /><br />
<?php
}
That will make a thumbnail visible in the user end instead of a number :)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745348577a4623687.html
评论列表(0条)