wp_upload_dir()
Проверено на WordPress 6.9, обновлено Источник: WordPress Developer Resources.
Сигнатура
wp_upload_dir( string|null $time = null, bool $create_dir = true, bool $refresh_cache = false ): array
Описание
Проверяется опция «upload_path», которая должна задаваться относительно корневой папки сайта, и если она не пуста, то используется. Если она пуста, путь будет «WP_CONTENT_DIR/uploads». Если определена константа «UPLOADS», она переопределяет опцию «upload_path» и путь «WP_CONTENT_DIR/uploads».
URL-путь загрузок задаётся либо опцией «upload_url_path», либо с помощью константы «WP_CONTENT_URL» с добавлением «/uploads» к пути.
Если опция «uploads_use_yearmonth_folders» включена (флажок в панели настроек администрирования), используется дата. Формат: сначала год, затем месяц.
Если путь не удалось создать, возвращается ошибка с ключом «error», содержащим сообщение об ошибке. Ошибка указывает на то, что родительский каталог недоступен серверу для записи.
Оригинал (английский)
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.
Параметры
$time
string|null
необязательный
= null
$create_dir
bool
необязательный
= true
$refresh_cache
bool
необязательный
= false
Возвращаемое значение
array
Исходный код
wp-includes/functions.php:2381
function wp_upload_dir( $time = null, $create_dir = true, $refresh_cache = false ) {
static $cache = array(), $tested_paths = array();
$key = sprintf( '%d-%s', get_current_blog_id(), (string) $time );
if ( $refresh_cache || empty( $cache[ $key ] ) ) {
$cache[ $key ] = _wp_upload_dir( $time );
}
/**
* Filters the uploads directory data.
*
* @since 2.0.0
*
* @param array $uploads {
* Array of information about the upload directory.
*
* @type string $path Base directory and subdirectory or full path to upload directory.
* @type string $url Base URL and subdirectory or absolute URL to upload directory.
* @type string $subdir Subdirectory if uploads use year/month folders option is on.
* @type string $basedir Path without subdir.
* @type string $baseurl URL path without subdir.
* @type string|false $error False or error message.
* }
*/
$uploads = apply_filters( 'upload_dir', $cache[ $key ] );
if ( $create_dir ) {
$path = $uploads['path'];
if ( array_key_exists( $path, $tested_paths ) ) {
$uploads['error'] = $tested_paths[ $path ];
} else {
if ( ! wp_mkdir_p( $path ) ) {
if ( str_starts_with( $uploads['basedir'], ABSPATH ) ) {
$error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir'];
} else {
$error_path = wp_basename( $uploads['basedir'] ) . $uploads['subdir'];
}
$uploads['error'] = sprintf(
/* translators: %s: Directory path. */
__( 'Unable to create directory %s. Is its parent directory writable by the server?' ),
esc_html( $error_path )
);
}
$tested_paths[ $path ] = $uploads['error'];
}
}
return $uploads;
}
История изменений
| Версия | Описание |
|---|---|
| 2.0.0 | Introduced. |

