I am trying to use WordPress'using WordPress Media Library in my plugin, making it upload to plugin's images folder. It will be used on my plugin's in the backend page. How to change the upload path for it dynamically? Can it be done during the enqueue, or when creating it in JavaScript?
So far I've been making workarounds using upload_dir
filter. I've managed to make it upload to change the proper folderdirectory when uploading using
add_filter( 'plupload_default_params', array( $this, 'plupload_default_params_filter' ) );
function plupload_default_params_filter($params) {
//check if it's the plugin form
if(isset($_GET['type']) && isset($_GET['action']) && 'edit' == $_GET['action']){
$params['subfolder'] = $_GET['type'];
}
return $params;
}
which causes the ajax $_POST
array to contain subfolder
key so I can check it in upload_dirplupload_default_params
filter:
function change_upload_dir( $args ) {
if(isset($_POST['subfolder']) {
//change upload dir accordingly
}
}
}
This works fine for uploading through drag-and-drop, but when the attachments are loaded inI don't know how to hook into the library (through AJAX query-attachments
action), I have no way of knowing if it's called from that queries the plugin media library, so it defaults to uploads/year/month
.
Is there any way to change it for a certain Media Library object? I don't want to changefiles into the upload_dir globally, as I want blog posts' attachments to go to default locationlibrary.