WordPress at a glance

get_attached_file() WP 1.0

Retrieve attached file path based on attachment ID.

By default the path will go through the 'get_attached_file' filter, but passing a true to the $unfiltered argument of get_attached_file() will return the file path unfiltered.

The function works by getting the single post meta name, named '_wp_attached_file' and returning it. This is a convenience function to prevent looking up the meta name and provide a mechanism for sending the attached filename through a filter.

Works based on: get_post_meta()
✈ 1 time = 0.000594s = slow | 50000 times = 0.41s = very fast PHP 7.0.8, WP 4.6

No Hooks.

Return

String/false. The file path to where the attached file should be, false otherwise.

Usage

get_attached_file( $attachment_id, $unfiltered );
$attachment_id(int) (required)
Attachment ID.
$unfiltered(true/false)
Whether to apply filters.
Default: false

Code of get_attached_file: wp-includes/post.php VER 5.0

<?php
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 && 0 !== strpos( $file, '/' ) && ! preg_match( '|^.:\\\|', $file ) && ( ( $uploads = wp_get_upload_dir() ) && 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 $file          Path to attached file.
	 * @param int    $attachment_id Attachment ID.
	 */
	return apply_filters( 'get_attached_file', $file, $attachment_id );
}

Related Functions

From tag: path (directory folder)

More from category: Attachments (Media files)

No comments
    Hello, !     Log In . Register