How to set additional parameter in wp.media?

I’ve been searching for this answer too and found it using this answer. You just need to add a property to the library object:

var wpMedia = wp.media.frames.file_frame = wp.media({
    'library': {
        type: 'image',
        custom_var: 'webhead'
    },
    'multiple': true
});

Then in PHP you can see the custom variable in the ajax_query_attachments_args hook like so:

function webhead_ajax_query_attachments_args() {
    var_dump($_REQUEST['query']['custom_var']);
    exit;
}
add_filter( 'ajax_query_attachments_args', 'webhead_ajax_query_attachments_args' );

Leave a Comment