1. 首页
  2. WordPress钩子手册

get_attached_file

apply_filters( ‘get_attached_file’, string $file, int $attachment_id )

过滤器::根据给定的ID过滤附加文件。
Filter Hook: Filters the attached file based on the given ID.

目录锚点:#参数#源码


参数(Parameters)

参数 类型 说明
$file (string) 附加文件的路径。
$attachment_id (int) 附件ID。

源码(Source)

/**
 * 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.
 *
 * @since 2.0.0
 *
 * @param int  $attachment_id Attachment ID.
 * @param bool $unfiltered    Optional. Whether to apply filters. Default false.
 * @return string|false The file path to where the attached file should be, false otherwise.
 */
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_upload_dir()) && false === $uploads['error'] ) )
		$file = $uploads['basedir'] . "/$file";
	if ( $unfiltered )
		return $file;

	/**
	 * Filter 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 );
}
更新版本 源码位置 使用 被使用
2.1.0 wp-includes/post.php:521 1 0

get_attached_file 为WP2原创文章,链接:https://www.wp2.cn/hook/get_attached_file/