返回介绍

get_attached_file()

发布于 2017-09-10 22:44:43 字数 4824 浏览 1279 评论 0 收藏 0

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

Retrieve attached file path based on attachment ID.


description

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.


参数

$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.


源代码

File: wp-includes/post.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 );
}

更新日志

Versiondescription
2.0.0Introduced.

相关函数

Uses

  • wp-includes/functions.php: wp_get_upload_dir()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/post.php: get_post_meta()
  • wp-includes/post.php: get_attached_file

Used By

  • wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::_validate_header_video()
  • wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::import_theme_starter_content()
  • wp-admin/includes/class-wp-media-list-table.php: WP_Media_List_Table::column_title()
  • wp-includes/post.php: wp_attachment_is()
  • wp-admin/includes/class-file-upload-upgrader.php: File_Upload_Upgrader::__construct()
  • wp-admin/includes/image-edit.php: wp_restore_image()
  • wp-admin/includes/image-edit.php: wp_save_image()
  • wp-admin/includes/image.php: _copy_image_file()
  • wp-admin/includes/image.php: _load_image_to_edit_path()
  • wp-admin/includes/image.php: wp_crop_image()
  • wp-admin/includes/media.php: attachment_submitbox_metadata()
  • wp-admin/includes/media.php: get_media_item()
  • wp-admin/custom-header.php: Custom_Image_Header::step_2()
  • wp-admin/custom-header.php: Custom_Image_Header::step_3()
  • wp-includes/deprecated.php: wp_load_image()
  • wp-includes/deprecated.php: get_attachment_icon_src()
  • wp-includes/post-template.php: wp_get_attachment_link()
  • wp-includes/media.php: wp_maybe_generate_attachment_metadata()
  • wp-includes/media.php: wp_prepare_attachment_for_js()
  • wp-includes/media.php: get_attachment_taxonomies()
  • wp-includes/post.php: wp_mime_type_icon()
  • wp-includes/post.php: wp_delete_attachment()
  • wp-includes/post.php: wp_get_attachment_thumb_file()
  • Show 18 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Examples

    
    $fullsize_path = get_attached_file( $attachment_id ); // Full path
    $filename_only = basename( get_attached_file( $attachment_id ) ); // Just the file name
    
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文