W3cubDocs

/WordPress

update_attached_file( int $attachment_id, string $file )

Update attachment file path based on attachment ID.

Description

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

Parameters

$attachment_id

(int) (Required) Attachment ID.

$file

(string) (Required) File path for the attachment.

Return

(bool) True on success, false on failure.

Source

File: wp-includes/post.php

function update_attached_file( $attachment_id, $file ) {
	if ( ! get_post( $attachment_id ) ) {
		return false;
	}

	/**
	 * Filters 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' );
	}
}

Changelog

Version Description
2.1.0 Introduced.

© 2003–2019 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/update_attached_file