update_attached_file()更新附件(图片/文件)的路径

目录

描述

译文

按附件编号更新附件文件的路径。

用于更新附件的文件路径,用文章meta名'_wp_attached_file'存储附件路径

原文

Update attachment file path based on attachment ID.

Used to update the file path of the attachment, which uses post meta name '_wp_attached_file' to store the path of the attachment.

Note: This does not move the file. This simply updates the '_wp_attached_file' post meta after a move.

update_attached_file() 描述

用法

<?php update_attached_file$attachment_id$file ?>

update_attached_file() 用法

参数

$attachment_id

(integer) (必填) Attachment ID

默认值: None

$file

(string) (必填) File path for the attachment

默认值: None

update_attached_file() 参数

返回值

(boolean) 

False on failure, true on success.

update_attached_file() 返回值

注意

  • 使用到: apply_filters() to add update_attached_file() on $file and $attachment_id.

update_attached_file() 注意

历史

添加于 版本: 2.1.0

update_attached_file() 历史

源文件

update_attached_file() 函数的代码位于 wp-includes/post.php

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Update attachment file path based on attachment ID.
 *
 * Used to update the file path of the attachment, which uses post meta name
 * '_wp_attached_file' to store the path of the attachment.
 *
 * @since 2.1.0
 *
 * @param int    $attachment_id Attachment ID.
 * @param string $file          File path for the attachment.
 * @return bool True on success, false on failure.
 */
function update_attached_file( $attachment_id, $file ) {
	if ( !get_post( $attachment_id ) )
		return false;

	/**
	 * Filter the path to the attached file to update.
	 *
	 * @since 2.1.0
	 *
	 * @param string $file          Path to the attached file to update.
	 * @param int    $attachment_id Attachment ID.
	 */
	$file = apply_filters( 'update_attached_file', $file, $attachment_id );

	if ( $file = _wp_relative_upload_path( $file ) )
		return update_post_meta( $attachment_id, '_wp_attached_file', $file );
	else
		return delete_post_meta( $attachment_id, '_wp_attached_file' );
}

update_attached_file() 源文件