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¶meter=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