2

I have this code.

$('.custom-browse-button').on('click', function(e) {
    e.preventDefault();

    var wpMedia = wp.media.frames.file_frame = wp.media({
        'library': {
            type: 'image'
        },
        'multiple': true
    });
    wpMedia.open()
        .on('select', function(e) {
            var uploaded_image = wpMedia.state().get('selection').toJSON();
            // already done with select.
            console.log(uploaded_image);
        });
});

In ajax request there are these parameters in the method POST:

action:query-attachments
post_id:0
query[post_mime_type]:image
query[orderby]:date
query[order]:DESC
query[posts_per_page]:40
query[paged]:1

I want to add something more fore checking and hooks. Example: my_filtered_user_id=3 OR media_width_eqm=1000

How to add these things in wp.media?

Update: Now I am able to send parameter to upload hooks using this answer.
However I still unable to send GET or POST data to browse media hooks (ajax_query_attachments_args) using that answer.

$('.custom-browse-button').on('click', function(e) {
    e.preventDefault();

    var wpMedia = wp.media.frames.file_frame = wp.media({
        'library': {
            type: 'image'
        },
        'multiple': true
    });
    wpMedia.uploader.options.uploader.params.my_filtered_user_id = 3;
    wpMedia.open()
        .on('select', function(e) {
            var uploaded_image = wpMedia.state().get('selection').toJSON();
            // already done with select.
            console.log(uploaded_image);
        });
});

1 Answer 1

4

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' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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