wp_get_upload_dir()
Retrieves data about the uploads folder (upload) as an array of parameters.
This is exactly the same function as wp_upload_dir(), but less "resource-demanding" — it does not try to create the uploads folder if such a folder does not exist.
The function should be used in themes when only the data is needed: basedir and baseurl — i.e. when you don't need to upload a file, but only need to get the uploads directory data.
Multisite
For Multisite the function will return the uploads folder data for the current site. For example:
[basedir] => /var/app/www/wp-content/uploads/sites/2 [baseurl] => https://example.com/wp-content/uploads/sites/2
Read more about this function in the description of wp_upload_dir().
The uploads folder is usually called upload and is located in the content folder wp-content.
No Hooks.
Returns
Array. Array of uploads directory data:
array ( 'path' => '/home/example.com/public_html/wp-content/uploads/2016/04', 'url' => 'http://example.com/wp-content/uploads/2016/04', 'subdir' => '/2016/04', 'basedir' => '/home/example.com/public_html/wp-content/uploads', 'baseurl' => 'http://example.com/wp-content/uploads', 'error' => false, )
where
-
path- server path to the uploads directory including year and month if the corresponding option is enabled in the admin. -
url- URL of the uploads directory including year and month if the corresponding option is enabled in the admin. -
subdir- name of the final uploads subdirectory inside wp-content/uploads. -
basedir- server path to the common uploads directory. -
baseurl- URL to the common uploads directory. error- in case of an error when retrieving data the array element error will be equal to true.
Usage
$upload_dir = wp_get_upload_dir();
Examples
#1 Get the download directory data
$dir = wp_get_upload_dir(); print_r( $dir ); /* Will display Array ( [path] => /home/example.com/public_html/wp-content/uploads/2016/04 [url] => http://example.com/wp-content/uploads/2016/04 [subdir] => /2016/04 [basedir] => /home/example.com/public_html/wp-content/uploads [baseurl] => http://example.com/wp-content/uploads [error] => ) */ // Display the data echo $dir['basedir']; // /home/example.com/public_html/wp-content/uploads echo $dir['baseurl']; // http://example.com/wp-content/uploads
For multisite the data will be like:
Array ( [path] => /var/app/www/wp-content/uploads/sites/2/2023/09 [url] => http://example.com/wp-content/uploads/sites/2/2023/09 [subdir] => /2023/09 [basedir] => /var/app/www/wp-content/uploads/sites/2 [baseurl] => http://example.com/wp-content/uploads/sites/2 [error] => )
#2 Another example of a download folder
See the description of wp_upload_dir().
Notes
- See: wp_upload_dir()
Changelog
| Since 4.5.0 | Introduced. |
wp_get_upload_dir() wp get upload dir code WP 6.9
function wp_get_upload_dir() {
return wp_upload_dir( null, false );
}