I am trying to change the path to my uploads directory when on this one specific page in my custom plugin. The page is a standard php page and not any specific type of post type page.
I have found numerous articles explaining the process, and I can see how it would work if this were a custom post type, but seeing as it’s not the examples are not working as intended.
The URL to my custom page is as follows:
http://localhost/custom-plugin/wp-admin/admin.php?page=custom-plugin-page&id=1
.
the $_POST variable is dynamic based on the item that the user is editing.
I have come across the following and adjusted it to my needs:
function edd_load_upload_filter() {
global $pagenow;
if ( ! empty( $_POST['page'] ) && $_POST['page'] == 'custom-plugin-page' && ( 'async-upload.php' == $pagenow || 'media-upload.php' == $pagenow ) ) {
add_filter( 'upload_dir', 'edd_set_upload_dir' );
}
}
add_action('admin_init', 'edd_load_upload_filter');
function edd_set_upload_dir($upload) {
$upload['subdir'] = '/edd' . $upload['subdir'];
$upload['path'] = $upload['basedir'] . $upload['subdir'];
$upload['url'] = $upload['baseurl'] . $upload['subdir'];
return $upload;
}
But as you can see, the function is checking if the post type is type ‘download’.
From what I can tell, the $_POST variable is not empty on initial page load, but when the media modal is opened it is empty. Since it returns as empty inside the media modal, the path to the upload directory does not properly get set. If I remove the !empty( $_POST[
check the path is properly adjusted, but then it gets adjusted across the entire site and not just on my custom page.
page'] );
Not sure why the examples on the net all use some $_POST or $_REQUEST variable, but when I go to use it, it is empty so my function never fires. Any ideas?