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[
page'] );
check the path is properly adjusted, but then it gets adjusted across the entire site and not just on my custom 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?

2 Answers
2

After working on this for a bit and checking the GLOBALS variable for anything useful, it looks like the referring URL inside of the media modal is the same URL as my custom plugin page.

Using that and splitting it up a bit I was able to confirm that I am on the approrpriate page. I’m sure that there are other, more elegant solutions out there, but this is what I was able to come up with. I have tested and it seems to be working properly.

Here is my final solution :

    add_filter( 'admin_init' , 'check_if_we_should_change_upload_dir', 999 );
    function check_if_we_should_change_upload_dir() {   
            global $pagenow;
            $referrer = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '';
            if( $referrer != '' ) {
                $explode_1 = explode( 'page=" , $referrer );
                if( isset( $explode_1[1] ) ) {
                    $referring_page = explode( "&id=' , $explode_1[1] );
                    if( isset( $referring_page[0] ) && $referring_page[0] == 'custom-plugin-page' && ( 'async-upload.php' == $pagenow || 'media-upload.php' == $pagenow ) ) {
                        add_filter( 'upload_dir', 'alter_the_upload_dir' );
                    }
                }
            }
       }


  function alter_the_upload_dir( $upload ) {
        $upload['subdir'] = '/custom-directory' . $upload['subdir'];
        $upload['path'] = $upload['basedir'] . $upload['subdir'];
        $upload['url']  = $upload['baseurl'] . $upload['subdir'];
        return $upload;
    }

If anyone has any better solutions to checking, I am all ears!

Evan

Leave a Reply

Your email address will not be published. Required fields are marked *