wp_upload_dir()
Get an array containing the current upload directory's path and url.
Checks the 'upload_path' option, which should be from the web root folder, and if it isn't empty it will be used. If it is empty, then the path will be 'WP_CONTENT_DIR/uploads'. If the 'UPLOADS' constant is defined, then it will override the 'upload_path' option and 'WP_CONTENT_DIR/uploads' path.
The upload URL path is set either by the 'upload_url_path' option or by using the 'WP_CONTENT_URL' constant and appending '/uploads' to the path.
If the 'uploads_use_yearmonth_folders' is set to true (checkbox if checked in the administration settings panel), then the time will be used. The format will be year first and then month.
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.
Hooks from the function
Return
Array
. Array of information about the upload directory.
Usage
wp_upload_dir( $time, $create_dir, $refresh_cache );
- $time(string|null)
- Time formatted in 'yyyy/mm'.
Default: null - $create_dir(true|false)
- Whether to check and create the uploads directory.
Default: true for backward compatibility - $refresh_cache(true|false)
- Whether to refresh the cache.
Default: false
Examples
#1 What the array returns
$upload_dir = wp_upload_dir(); print_r( $upload_dir ); // will return: /* Array ( [path] => /home/k/wp-example.com/public_html/wp-content/uploads/2014/02 [url] => http://wp-example.com/wp-content/uploads/2014/02 [subdir] => /2014/02 [basedir] => /home/k/wp-example.com/public_html/wp-content/uploads [baseurl] => http://wp-example.com/wp-content/uploads [error] => ) */
#2 The URL of the WordPress download directory
Get the URL of the WordPress downloads directory:
$upload_dir = wp_upload_dir(); echo $upload_dir['baseurl']; //> http://wp-example.com/wp-content/uploads
#3 Getting all elements as an object
It is more convenient to get elements in the form of an object, so it is convenient to work with them:
$upload_dir = (object) wp_upload_dir(); if( ! $upload_dir->error ){ echo $upload_dir->path ."\n"; echo $upload_dir->url ."\n"; echo $upload_dir->subdir ."\n"; echo $upload_dir->basedir ."\n"; echo $upload_dir->baseurl ."\n"; } /* we get: /home/wp-example.com/public_html/wp-content/uploads/2015/08 http://wp-example.com/wp-content/uploads/2015/08 /2015/08 /home/wp-example.com/public_html/wp-content/uploads http://wp-example.com/wp-content/uploads */
Changelog
Since 2.0.0 | Introduced. |