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?


2 Answers 2


If 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) {
                var button = $(this);
                var id = button.prev();
                wp.media.editor.send.attachment = function(props, attachment) {
                return false;

Use this html:

    <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>
  • 1
    Perfect! I just wants to replace "$" with "jQuery" and everything works as expected! Thank you.
    – Libin
    Commented Dec 31, 2014 at 11:01
  • No need for is_admin() when you use the hook admin_enqueue_scripts. Also, I would check if you're on the correct page with get_current_screen().
    – Bjorn
    Commented Aug 6, 2019 at 21:28
  • For anyone who, like me, may also need the URL for the image, you can use the following: var attachmentURL = wp.media.attachment(attachment.id).get("url");. I put this inside function(props, attachment)
    – seveninstl
    Commented Mar 6, 2020 at 18:01
  • @seveninstl I use Worpdress 5.6 and attachment.url works. Is there any reason for using wp.media.attachment(attachment.id).get("url") ?
    – leaf
    Commented Dec 10, 2020 at 9:44

Show thumbnail preview instead of number

Just as a tweak, I did this...

changed the number input to hidden.


$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 />

That will make a thumbnail visible in the user end instead of a number :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.