Setting up WordPress wp.media

I want the user to click on a button that opens a media window, only posts loaded on a specific page.

Then I want their selection to populate the input field.

Right now I am struggling to find an intermediate point between the "post" type and the "select" frame type.

If I set the wp.media frame to "post" I can see the images uploaded to the post, but I cannot insert them into the input field, and if I set "select" I cannot restrict the "uploaded to mail" option.

var uploader;

$j(document).on('click','input[name="uploader_submit"]',function(){

    var button      = $j(this);
    var parent      = button.parents('.uploader');
    var input       = $j('input:text:first',parent);

    /* Extend the wp.media object */

    uploader = wp.media.frames.file_frame = wp.media({
        title: "Choose Media",
        button: { text: "Choose Media" },
        multiple: false,
        library: { type: "image" },
        frame: "post"
    });

    /* When a file is selected, grab the URL and set it as the text field value */

    uploader.on('select',function(){            

        attachment = uploader.state().get('selection').first().toJSON();
        input.val(attachment.url);

    });

    /* Open the uploader dialog */

    uploader.open();

    return false;

});

      

+3


source to share


1 answer


Use it like this:



/* Extend the wp.media object */

uploader = wp.media.frames.file_frame = wp.media({
    title: "Choose Media",
    button: { text: "Choose Media" },
    multiple: false,
    library: { type: "image" },
    frame: "post",
    state: "insert"

});

/* When a file is selected, grab the URL and set it as the text field value */

uploader.on('insert',function(){    

      

0


source







All Articles