update_attached_file

The timeline below displays how wordpress function update_attached_file has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Updates 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;
    }
    /**
     * 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);
    $file = _wp_relative_upload_path($file);
    if ($file) {
        return update_post_meta($attachment_id, '_wp_attached_file', $file);
    } else {
        return delete_post_meta($attachment_id, '_wp_attached_file');
    }
}

WordPress Version: 5.3

/**
 * 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;
    }
    /**
     * 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);
    $file = _wp_relative_upload_path($file);
    if ($file) {
        return update_post_meta($attachment_id, '_wp_attached_file', $file);
    } else {
        return delete_post_meta($attachment_id, '_wp_attached_file');
    }
}

WordPress Version: 4.6

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

WordPress Version: 4.0

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

WordPress Version: 3.9

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

WordPress Version: 3.7

/**
 * 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
 * @uses apply_filters() Calls 'update_attached_file' on file path and attachment ID.
 *
 * @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;
    }
    $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');
    }
}