get_attached_file()

最后更新于:2021-11-26 04:24:14

get_attached_file( int$attachment_id, bool$unfiltered=false)

Retrieve attached file path based on attachment ID.

参数

$attachment_id

(int) (Required) Attachment ID.

$unfiltered

(bool) (Optional) Whether to apply filters.

Default value: false

响应

(string|false) The file path to where the attached file should be, false otherwise.

源文件

文件: gc-includes/post.php

function get_attached_file( $attachment_id, $unfiltered = false ) {
	$file = get_post_meta( $attachment_id, '_gc_attached_file', true );

	// If the file is relative, prepend upload dir.
	if ( $file && 0 !== strpos( $file, '/' ) && ! preg_match( '|^.:\|', $file ) ) {
		$uploads = gc_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 );
}