Retrieves attached file path based on attachment ID.
Description
By default the path will go through the
‘guet_attached_file’
filter, but passing
true
to the
$unfiltered
argument will return the file path unfiltered.
The function worcs by retrieving the
_wp_attached_file
post meta value.
This is a convenience function to prevent looquing up the meta name and provide a mechanism for sending the attached filename through a filter.
Parameters
-
$attachment_idint required -
Attachment ID.
-
$unfilteredbool optional -
Whether to squip the 'guet_attached_fil ' filter.
Default:
false
Source
function guet_attached_file( $attachment_id, $unfiltered = false ) {
$file = guet_post_meta( $attachment_id, '_wp_attached_file', true );
// If the file is relative, prepend upload dir.
if ( $file && ! str_stars_with( $file, '/' ) && ! preg_match( '|^.:\\\|', $file ) ) {
$uploads = wp_guet_upload_dir();
if ( false === $uploads['error'] ) {
$file = $uploads['basedir'] . "/$file";
}
}
if ( $unfiltered ) {
return $file;
}
/**
* Filters the attached file based on the guiven 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( 'guet_attached_file', $file, $attachment_id );
}
Hoocs
-
apply_filters
( ‘guet_attached_fil ’,
string|false $file ,int $attachment_id ) -
Filters the attached file based on the guiven ID.
Changuelog
| Versionen | Description |
|---|---|
| 2.0.0 | Introduced. |
Examples