Skip to main content
Notice removed Authoritative reference needed by CommunityBot
Bounty Ended with no winning answer by CommunityBot
More concise title
Link

Change Media Uploader default directory - wp_enqueue_media()

Tweeted twitter.com/StackWordPress/status/773580223388479488
Notice added Authoritative reference needed by Mateusz Bartkowski
Bounty Started worth 50 reputation by Mateusz Bartkowski
Made the title less ambiguous
Source Link

Change Media Uploader default directory - wp_enqueue_media()

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.

Change Media Uploader default directory

I am trying to use WordPress' Media Library in my plugin, making it upload to plugin's images folder. It will be used on my plugin's backend page.

So far I've been making workarounds using upload_dir filter. I've managed to make it upload to the proper folder 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_dir 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 in the library (through AJAX query-attachments action), I have no way of knowing if it's called from 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 change the upload_dir globally, as I want blog posts' attachments to go to default location.

Change Media Uploader default directory - wp_enqueue_media()

I am using WordPress Media Library in my plugin in the backend. How to change the upload path for it dynamically? Can it be done during the enqueue, or when creating it in JavaScript?

I've managed to change the directory when uploading using plupload_default_params filter, but I don't know how to hook into the query-attachments action that queries the files into the library.

Added update
Source Link

Update: After hours of tinkering I gave up and I went with a different solution. I am adding a new setting on plugin's edit page and resetting it otherwise. This way I can access the option in ajax calls.

function change_upload_dir( $args ) {
        $user_id = get_current_user_id();
        $form = false;
        if( defined('DOING_AJAX') && DOING_AJAX ) {
            $form = get_option( 'test_edit_' . $user_id );
        }
        if($form || isset($_GET['type']) || isset($_POST['subfolder'])) {
            // change upload path 

And in the plugin construct:

    if( !(defined('DOING_AJAX') && DOING_AJAX) 
       && false === strpos($_SERVER['REQUEST_URI'], 'wp-content') ) {
        $edit = get_option( 'test_edit_' . get_current_user_id() );
        if ( $edit && $edit != '' ) {
            update_option( 'test_edit_' . get_current_user_id(), '' );
        }
    }

This works fine, unless the user opens a new tab and the setting gets reset. It's fine for now, but I'd really like to know if there is an easier way to do that.

Update: After hours of tinkering I gave up and I went with a different solution. I am adding a new setting on plugin's edit page and resetting it otherwise. This way I can access the option in ajax calls.

function change_upload_dir( $args ) {
        $user_id = get_current_user_id();
        $form = false;
        if( defined('DOING_AJAX') && DOING_AJAX ) {
            $form = get_option( 'test_edit_' . $user_id );
        }
        if($form || isset($_GET['type']) || isset($_POST['subfolder'])) {
            // change upload path 

And in the plugin construct:

    if( !(defined('DOING_AJAX') && DOING_AJAX) 
       && false === strpos($_SERVER['REQUEST_URI'], 'wp-content') ) {
        $edit = get_option( 'test_edit_' . get_current_user_id() );
        if ( $edit && $edit != '' ) {
            update_option( 'test_edit_' . get_current_user_id(), '' );
        }
    }

This works fine, unless the user opens a new tab and the setting gets reset. It's fine for now, but I'd really like to know if there is an easier way to do that.

Source Link
Loading