get_attached_file()
Проверено на WordPress 6.9, обновлено Источник: WordPress Developer Resources.
Сигнатура
get_attached_file( int $attachment_id, bool $unfiltered = false ): string|false
Описание
По умолчанию путь проходит через фильтр ‘get_attached_file’, но передача true в аргумент $unfiltered вернёт путь к файлу без фильтрации.
Функция работает, получая значение метаполя записи _wp_attached_file.
Это вспомогательная функция, позволяющая не искать имя метаполя и предоставляющая механизм передачи имени прикреплённого файла через фильтр.
Оригинал (английский)
By default the path will go through the ‘get_attached_file’ filter, but passing true to the $unfiltered argument will return the file path unfiltered.
The function works by retrieving the _wp_attached_file post meta value.
This is a convenience function to prevent looking up the meta name and provide a mechanism for sending the attached filename through a filter.
Параметры
$attachment_id
int
обязательный
$unfiltered
bool
необязательный
= false
Возвращаемое значение
string|false
Исходный код
wp-includes/post.php:845
function get_attached_file( $attachment_id, $unfiltered = false ) {
$file = get_post_meta( $attachment_id, '_wp_attached_file', true );
// If the file is relative, prepend upload dir.
if ( $file && ! str_starts_with( $file, '/' ) && ! preg_match( '|^.:\\\|', $file ) ) {
$uploads = wp_get_upload_dir();
if ( false === $uploads['error'] ) {
$file = $uploads['basedir'] . "/$file";
}
}
if ( $unfiltered ) {
return $file;
}
/**
* Filters the attached file based on the given ID.
*
* @since 2.1.0
*
* @param string|false $file The file path to where the attached file should be, false otherwise.
* @param int $attachment_id Attachment ID.
*/
return apply_filters( 'get_attached_file', $file, $attachment_id );
}
История изменений
| Версия | Описание |
|---|---|
| 2.0.0 | Introduced. |

