wordpress

Access WordPress inbuilt filemanager for custom plugins


## HTML

<a href="#" class="custom_media_upload">Upload</a>
<img class="custom_media_image" src="" />
<input class="custom_media_url" type="text" name="attachment_url" value="">
<input class="custom_media_id" type="text" name="attachment_id" value="">

## PHP

if(function_exists( 'wp_enqueue_media' )){
wp_enqueue_media();
}

## JS

jQuery(document).ready(function($) {


var custom_uploader;
jQuery('.custom_media_upload').click(function(e) {

e.preventDefault();

custom_uploader = wp.media({
      title: 'Custom Title',
      button: {
      text: 'Custom Button Text'
  },
  multiple: true // Set this to true to allow multiple files to be selected
})
.on('select', function(el, bl) {

   var attachment = custom_uploader.state().get('selection').first().toJSON();
   console.log(custom_uploader.state());

   $('.custom_media_image').attr('src', attachment.url);
   $('.custom_media_url').val(attachment.url);
   $('.custom_media_id').val(attachment.id);
})
.open();
return false;
});
});
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s