6

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 am using wp_enqueue_media() in admin_enqueue_scripts action, and later creating the media frame in Javascript using wp.media.

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.

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.

2
  • By "it can be a good alternative to use in a plugin." I meant an alternative to wp_enqueue_media() that can be used in a plugin. Commented Sep 6, 2016 at 16:44
  • Why not to move the file after it was uploaded? Commented Sep 12, 2016 at 18:06

2 Answers 2

1

Might wanna look at

https://developer.wordpress.org/reference/functions/wp_upload_dir/

In definition there is a hook upload_dir, you can use that to change path.

Haven't tried or tested it, but you can give it a try.

1
  • I am using upload_dir hook to run my change_upload_dir function, but unfortunately it doesn't work for Media Library's AJAX queries and uploads and I don't want to set it globally, as I want standard upload directory for posts. Commented Sep 12, 2016 at 16:26
0

In order to change the default media upload location, you need to edit the wp-config.php file located in the root directory of your WordPress installation. If you want upload directory to be wp-content/files then you will need to place the following code in wp-config.php

define( 'UPLOADS', 'wp-content/'.'files' );

If you want the upload directory to be outside wp-content, like http://www.example.com/files/ then you need to set upload path in wp-config.php like this:

define( 'UPLOADS', ''.'files' );

Remember you can still choose whether or not you want uploaded files to be organized in month/year folders in Settings » Media.

Function Reference /wp_upload_dir – WordPress Codex

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.