Uploads folder

Upload folder can be defined in wp-config.php. From Editing wp-config.php:


Set the UPLOADS folder to media:

define( 'UPLOADS', 'wp-content/media' );

This path can not be absolute. It is always relative to ABSPATH, therefore does not require a leading slash. Add the define just before this:

 /** Sets up WordPress vars and included files. */
 require_once(ABSPATH . 'wp-settings.php');

If uploads path is not defined in wp-config.php it will be set to ‘_F9J_CONTENT_DIR/uploads’ by default or if WordPress version is less then 3.5 – to the value of the ‘upload_path’ option if it is defined.  Function _F9J_upload_dir() will return 

array(6) { ["path"]=> string(46) "/var/www/html/wp441/wp-content/uploads/2016/09" 
["url"]=> string(49) "http://localhost/wp441/wp-content/uploads/2016/09" 
["subdir"]=> string(8) "/2016/09" 
["basedir"]=> string(38) "/var/www/html/wp441/wp-content/uploads" ["baseurl"]=> string(41) "http://localhost/wp441/wp-content/uploads" ["error"]=> bool(false) }

wp441 is simply name of my WordPress installation on localhost.
From wordpress.org:


_F9J_upload_dir( string $time = nullbool $create_dir = truebool $refresh_cache = false )

Get an array containing the current upload directory’s path and url.
$time                      (string) (Optional) Time formatted in ‘yyyy/mm’.
Default value: null
$create_dir            (bool) (Optional) Whether to check and create the uploads directory. Default true for backward compatibility.
Default value: true
$refresh_cache      (bool) (Optional) Whether to refresh the cache.
Default value: false

If the path couldn’t be created, then an error will be returned with the key ‘error’ containing the error message. The error suggests that the parent directory is not writable by the server.

If in Dashboard on Settings->Media page  option “Organize my uploads into month- and year-based folders” is set then sub-folders which corresponds to the year  and month will be created.


Way to change upload path from stackexchange by kovshenin:

Nope. You should never change anything in the WordPress core files because all your changes will be lost during the next update. You should use actions and filters instead:

add_filter( 'pre_option_upload_path', function( $upload_path ) {
    return '/path/to/static';
});

add_filter( 'pre_option_upload_url_path', function( $upload_url_path ) {
    return 'http://static.example.org';
});

From wordpress.org:

upload dir
This hook allows you to change the directory where files are uploaded to. The keys and values in the array are used by the _F9J_upload_dir function in wordpress core, which is doing the work.

add_filter('upload_dir', 'awesome_wallpaper_dir');

function awesome_wallpaper_dir( $param ){
    $mydir = '/awesome';

    $param['path'] = $param['path'] . $mydir;
    $param['url'] = $param['url'] . $mydir;

    error_log("path={$param['path']}");  
    error_log("url={$param['url']}");
    error_log("subdir={$param['subdir']}");
    error_log("basedir={$param['basedir']}");
    error_log("baseurl={$param['baseurl']}");
    error_log("error={$param['error']}"); 

    return $param;
}

If your plugin is written as a class, you’ll want to hook to it like so:

add_filter('upload_dir', array(&$this,'awesome_wallpaper_dir'));

Using this, in conjunction with the _F9J_handle_upload_prefilter, you can dynamically determine which directory to upload to, based on the files you upload.

Leave a Reply

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