Open media box library from link

by Aftab   Last Updated April 26, 2018 15:08 PM

I am developing a plugin in which I want to open media library box after clicking on a link from Admin Panel just like we do for Choose featured image.

<a href="#">Choose Image</a>

After clicking on Choose Image I want that media library box displayed in thickbox.

Thanks



Answers 2


By enququing an JS file we can do like this:

<img src="#" id="img-src">
<a href="#" id="img-upload">Add Image</a>

Content of JS file

jQuery(document).ready(function(e){
    e.preventDefault();
    jQeury('#img-upload').click(function(){
        var upload = wp.media({
        title:'Choose Image', //Title for Media Box
        multiple:false //For limiting multiple image
        })
        .on('select', function(){
            var select = upload.state().get('selection');
            var attach = select.first().toJSON();
            console.log(attach.id); //the attachment id of image
            console.log(attach.url); //url of image
            jQuery('img#img-upload').attr('src',attach.url);
        })
        .open();
   });

}); 

Like this we can have it.

Aftab
Aftab
May 21, 2015 14:21 PM

<img src="#" id="img-src"><a href="#" id="img-upload">Add Image</a>


jQuery(document).ready(function(){
   jQuery('#img-upload').click(function(e){
        e.preventDefault();
        var upload = wp.media({
        title:'Choose Image', //Title for Media Box
        multiple:false //For limiting multiple image
        })
        .on('select', function(){
            var select = upload.state().get('selection');
            var attach = select.first().toJSON();
            console.log(attach.id); //the attachment id of image
            console.log(attach.url); //url of image
            jQuery('img#img-upload').attr('src',attach.url);
        })
        .open();
   });
});

wp_enqueue_media(); calls the scripts into the footer of the page.

Sam Prasanna
Sam Prasanna
April 26, 2018 15:01 PM

Related Questions


How to destroy or dispose wordpress uploder/manager?

Updated February 26, 2017 18:08 PM


WordPress Dropdown image select

Updated August 14, 2017 18:08 PM