Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to figure out a better way to pass a parameter to the function I am using with the upload_dir filter. Currently I have only gotten it to work by using sessions and/or cookie that I erase after I use them.

I have a URL like the following: /wp-admin/admin.php?page=my-plugin.php&parameter=apple

I want to upload the file into a directory called /apple/ (inside of the default upload directory) so that I can better categorize the images.

This may be too much info, but figured it might help understand my end goal. I built out the page in the plugin using the following so that I could use the media uploader from there. This basically just lets you pick images and once you click 'insert' it drops the image IDs as a comma sep list into a hidden variable.

$('#uploadGalleryButton').click(function(e) {
    e.preventDefault();

    //If the uploader object has already been created, reopen the dialog
    if (plugin_gallery_uploader) {
        plugin_gallery_uploader.open();
        return;
    }

    //Extend the wp.media object
    plugin_gallery_uploader = wp.media({
        multiple: true
    });

    plugin_gallery_uploader.on('select', function() {
        selectedImages = '';
        plugin_gallery_uploader.state().get('selection').each(function(item){
            selectedImages += item.id+',';
        });

        selectedImages = selectedImages.slice(0,-1);
        $('#imageList').val(selectedImages);
    });

    plugin_gallery_uploader.on('open',function() {
      var selection = plugin_gallery_uploader.state().get('selection');
      ids = $('#imageList').val().split(',');
        ids.forEach(function(id) {
          attachment = wp.media.attachment(id);
          attachment.fetch();
          selection.add( attachment ? [ attachment ] : [] );
        });
    });

    //Open the uploader dialog
    plugin_gallery_uploader.open();
});

I had found the media_upload_form_url filter, and I figured perhaps I could append the parameter name/value to the form URL and pass it along in that manner, but I can't seem to get it to work (or perhaps it doesn't even work to override what I think it does).

add_filter('plugin_upload_form_url', 'plugin_upload_form_url', 9);

function plugin_upload_form_url($form_action_url, $type) {
    return '/test/';
}

If I run the above it doesn't seem to alter the upload form URL. Am I misinterpreting what that filter is supposed to do, or am I using it incorrectly? That filter is defined in /wp-admin/includes/media.php

Thanks so much!
Levi

share|improve this question
    
the function name you've assigned to your filter, awesome_upload_form_url, is not the name of the function you've created, is that a typo here? –  Milo Jul 27 '13 at 2:35
    
oh yes, that was a typo here and not in the plugin, i renames things when i pasted them in. Sorry about that. –  Levi Jul 27 '13 at 2:49
    
maybe you can set the UPLOADS constant on admin page init based on $_GET('parameter') –  G. M. Jul 27 '13 at 13:27
    
I just gave that a try and it does work to override the upload dir on the page the gallery popup is on, but the media popup POSTS the file to a different page (async-upload.php) and the $_GET param doesn't get passed along to that. Thanks for the tip and info on the UPLOADS constant though. –  Levi Jul 27 '13 at 22:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.