WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?


share|improve this question
up vote 7 down vote accepted

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:

    if ($('.set_custom_images').length > 0) {
    if ( typeof wp !== 'undefined' && wp.media && wp.media.editor) {
        $('.wrap').on('click', '.set_custom_images', 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>
share|improve this answer
Perfect! I just wants to replace "$" with "jQuery" and everything works as expected! Thank you. – Libin Dec 31 '14 at 11:01

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 :)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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