For some reason, today PHP is doing my head in with regard to setting up some filters in WordPress, the issue is how to pass variables between the two functions which are filters?
I am looking to leverage the WordPress hook upload_dir
and pre-upload-ui
to provide a means upload items directly to the selected folder, but still leverage the inbuilt uploader.
The first function below sets the upload directory for the uploader, and when used with a pre-defined value such as _PLAY
uploads the files to the desired directory on the server - Great :)
if (!function_exists ('lf_media_uploader_dir')) {
function lf_media_uploader_dir ($upload) {
$lf_up_dir = '/_PLAY';
// I am trying to get the value of the selected directory
// from the function lf_upload_prefilter, as this is a means
// of hooking into the top of the media uploader.
$lf_up_dir = apply_filters ('lf-upload-filter');
$upload['subdir'] = $lf_up_dir;
$upload['path'] = $upload['basedir'] . $lf_up_dir;
$upload['url'] = $upload['baseurl'] . $lf_up_dir;
return $upload;
}
}
add_filter ('upload_dir', 'lf_media_uploader_dir', 99);
Now to tie into the uploader page, I have grabbed the hook pre-upload-ui
. In the following function it correctly displays the "pre" message so I know it is invoked. But no matter what I have tried, including global variables, I can not pass a value back from this function to lf_media_uploader_dir
.
Here is the second function, which currently sets a static value after displaying the message on the Media Uploader pane.
if (!function_exists ('lf_upload_prefilter')) {
function lf_upload_prefilter ($lf_dir_select) {
echo '<pre>Filter Fired pre-upload-ui</pre>';
//Set a dummy value, later provide a selector box.
$lf_dir_select = '/_PLAY';
}
}
add_filter('pre-upload-ui','lf_upload_prefilter',99);