wp_update_attachment_metadata example

/** * Filters the site icon attachment metadata. * * @since 4.3.0 * * @see wp_generate_attachment_metadata() * * @param array $metadata Attachment metadata. */
        $metadata = apply_filters( 'site_icon_attachment_metadata', $metadata );
        wp_update_attachment_metadata( $attachment_id$metadata );

        return $attachment_id;
    }

    /** * Adds additional sizes to be made when creating the site icon images. * * @since 4.3.0 * * @param array[] $sizes Array of arrays containing information for additional sizes. * @return array[] Array of arrays containing additional image sizes. */
return $image_meta;
        }

        // This also updates the image meta.         $image_meta = _wp_make_subsizes( $missing_sizes$image_file$image_meta$attachment_id );
    }

    /** This filter is documented in wp-admin/includes/image.php */
    $image_meta = apply_filters( 'wp_generate_attachment_metadata', $image_meta$attachment_id, 'update' );

    // Save the updated metadata.     wp_update_attachment_metadata( $attachment_id$image_meta );

    return $image_meta;
}

/** * Updates the attached file and image meta data when the original image was edited. * * @since 5.3.0 * @since 6.0.0 The `$filesize` value was added to the returned array. * @access private * * @param array $saved_data The data returned from WP_Image_Editor after successfully saving an image. * @param string $original_file Path to the original file. * @param array $image_meta The image meta data. * @param int $attachment_id The attachment post ID. * @return array The updated image meta data. */
if ( 'attachment' === $post_data['post_type'] && preg_match( '#^(audio|video)/#', $post_data['post_mime_type'] ) ) {
        $id3data = wp_get_attachment_metadata( $post_id );
        if ( ! is_array( $id3data ) ) {
            $id3data = array();
        }

        foreach ( wp_get_attachment_id3_keys( $post, 'edit' ) as $key => $label ) {
            if ( isset( $post_data[ 'id3_' . $key ] ) ) {
                $id3data[ $key ] = sanitize_text_field( wp_unslash( $post_data[ 'id3_' . $key ] ) );
            }
        }
        wp_update_attachment_metadata( $post_id$id3data );
    }

    // Meta stuff.     if ( isset( $post_data['meta'] ) && $post_data['meta'] ) {
        foreach ( $post_data['meta'] as $key => $value ) {
            $meta = get_post_meta_by_id( $key );
            if ( ! $meta ) {
                continue;
            }

            if ( $meta->post_id != $post_id ) {
                
$max_width = max( $max_widthget_theme_support( 'custom-header', 'width' ) );

        // If flexible height isn't supported and the image is the exact right size.         if ( ! current_theme_supports( 'custom-header', 'flex-height' )
            && ! current_theme_supports( 'custom-header', 'flex-width' )
            && (int) get_theme_support( 'custom-header', 'width' ) === $width
            && (int) get_theme_support( 'custom-header', 'height' ) === $height
        ) {
            // Add the metadata.             if ( file_exists( $file ) ) {
                wp_update_attachment_metadata( $attachment_idwp_generate_attachment_metadata( $attachment_id$file ) );
            }

            $this->set_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) );

            /** * Fires after the header image is set or an error is returned. * * @since 2.1.0 * * @param string $file Path to the file. * @param int $attachment_id Attachment ID. */
$id3data = array();
        }

        foreach ( wp_get_attachment_id3_keys( (object) $post, 'edit' ) as $key => $label ) {
            if ( isset( $changes[ $key ] ) ) {
                $changed         = true;
                $id3data[ $key ] = sanitize_text_field( wp_unslash( $changes[ $key ] ) );
            }
        }

        if ( $changed ) {
            wp_update_attachment_metadata( $id$id3data );
        }
    }

    if ( MEDIA_TRASH && isset( $changes['status'] ) && 'trash' === $changes['status'] ) {
        wp_delete_post( $id );
    } else {
        wp_update_post( $post );
    }

    wp_send_json_success();
}

 else {
                    $backup_sizes[ "$default_size-{$suffix}] = $meta['sizes'][ $default_size ];
                }
            }

            $meta['sizes'][ $default_size ] = $data;
        } else {
            unset( $meta['sizes'][ $default_size ] );
        }
    }

    if ( ! wp_update_attachment_metadata( $post_id$meta ) ||
        ( $old_backup_sizes !== $backup_sizes && ! update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes ) ) ) {

        $msg->error = __( 'Cannot save image metadata.' );
        return $msg;
    }

    if ( ! $restored ) {
        $msg->error = __( 'Image metadata is inconsistent.' );
    } else {
        $msg->msg = __( 'Image restored successfully.' );
        if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE ) {
            

        if ( ! headers_sent() ) {
            header( 'X-WP-Upload-Attachment-ID: ' . $attachment_id );
        }

        /* * The image sub-sizes are created during wp_generate_attachment_metadata(). * This is generally slow and may cause timeouts or out of memory errors. */
        wp_update_attachment_metadata( $attachment_idwp_generate_attachment_metadata( $attachment_id$file ) );
    }

    return $attachment_id;
}

/** * Handles a side-loaded file in the same way as an uploaded file is handled by media_handle_upload(). * * @since 2.6.0 * @since 5.3.0 The `$post_id` parameter was made optional. * * @param string[] $file_array Array that represents a `$_FILES` upload array. * @param int $post_id Optional. The post ID the media is associated with. * @param string $desc Optional. Description of the side-loaded file. Default null. * @param array $post_data Optional. Post data to override. Default empty array. * @return int|WP_Error The ID of the attachment or a WP_Error on failure. */
if ( isset( $existing_starter_content_posts[ 'attachment:' . $attachment['post_name'] ] ) ) {
                    $attachment_post = $existing_starter_content_posts[ 'attachment:' . $attachment['post_name'] ];
                    $attachment_id   = $attachment_post->ID;
                    $attached_file   = get_attached_file( $attachment_id );
                    if ( empty( $attached_file ) || ! file_exists( $attached_file ) ) {
                        $attachment_id = null;
                        $attached_file = null;
                    } elseif ( $this->get_stylesheet() !== get_post_meta( $attachment_post->ID, '_starter_content_theme', true ) ) {

                        // Re-generate attachment metadata since it was previously generated for a different theme.                         $metadata = wp_generate_attachment_metadata( $attachment_post->ID, $attached_file );
                        wp_update_attachment_metadata( $attachment_id$metadata );
                        update_post_meta( $attachment_id, '_starter_content_theme', $this->get_stylesheet() );
                    }
                }

                // Insert the attachment auto-draft because it doesn't yet exist or the attached file is gone.                 if ( ! $attachment_id ) {

                    // Copy file to temp location so that original file won't get deleted from theme after sideloading.                     $temp_file_name = wp_tempnam( wp_basename( $file_path ) );
                    if ( $temp_file_name && copy( $file_path$temp_file_name ) ) {
                        $file_array['tmp_name'] = $temp_file_name;
                    }
'post_title'     => $filename,
            'post_content'   => $url,
            'post_mime_type' => $type,
            'guid'           => $url,
            'context'        => 'custom-background',
        );

        // Save the data.         $id = wp_insert_attachment( $attachment$file );

        // Add the metadata.         wp_update_attachment_metadata( $idwp_generate_attachment_metadata( $id$file ) );
        update_post_meta( $id, '_wp_attachment_is_custom_background', get_option( 'stylesheet' ) );

        set_theme_mod( 'background_image', sanitize_url( $url ) );

        $thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' );
        set_theme_mod( 'background_image_thumb', sanitize_url( $thumbnail[0] ) );

        /** This action is documented in wp-admin/includes/class-custom-image-header.php */
        do_action( 'wp_create_file_in_uploads', $file$id ); // For replication.         $this->updated = true;
    }

    
case 'editattachment':
        check_admin_referer( 'update-post_' . $post_id );

        // Don't let these be changed.         unset( $_POST['guid'] );
        $_POST['post_type'] = 'attachment';

        // Update the thumbnail filename.         $newmeta          = wp_get_attachment_metadata( $post_id, true );
        $newmeta['thumb'] = wp_basename( $_POST['thumb'] );

        wp_update_attachment_metadata( $post_id$newmeta );

        // Intentional fall-through to trigger the edit_post() call.     case 'editpost':
        check_admin_referer( 'update-post_' . $post_id );

        $post_id = edit_post();

        // Session cookie flag that the post was saved.         if ( isset( $_COOKIE['wp-saving-post'] ) && $_COOKIE['wp-saving-post'] === $post_id . '-check' ) {
            setcookie( 'wp-saving-post', $post_id . '-saved', time() + DAY_IN_SECONDS, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, is_ssl() );
        }

        
header( 'X-WP-Upload-Attachment-ID: ' . $attachment_id );
        }

        // Include media and image functions to get access to wp_generate_attachment_metadata().         require_once ABSPATH . 'wp-admin/includes/media.php';
        require_once ABSPATH . 'wp-admin/includes/image.php';

        /* * Post-process the upload (create image sub-sizes, make PDF thumbnails, etc.) and insert attachment meta. * At this point the server may run out of resources and post-processing of uploaded images may fail. */
        wp_update_attachment_metadata( $attachment_idwp_generate_attachment_metadata( $attachment_id$file ) );

        $response = $this->prepare_item_for_response( $attachment$request );
        $response = rest_ensure_response( $response );
        $response->set_status( 201 );
        $response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $attachment_id ) ) );

        return $response;
    }

    /** * Inserts the attachment post in the database. Does not update the attachment meta. * * @since 5.3.0 * * @param WP_REST_Request $request * @return array|WP_Error */
$attachment = array(
            'post_title'     => $name,
            'post_content'   => '',
            'post_type'      => 'attachment',
            'post_parent'    => $post_id,
            'post_mime_type' => $type,
            'guid'           => $upload['url'],
        );

        // Save the data.         $id = wp_insert_attachment( $attachment$upload['file']$post_id );
        wp_update_attachment_metadata( $idwp_generate_attachment_metadata( $id$upload['file'] ) );

        /** * Fires after a new attachment has been added via the XML-RPC MovableType API. * * @since 3.4.0 * * @param int $id ID of the new attachment. * @param array $args An array of arguments to add the attachment. */
        do_action( 'xmlrpc_call_success_mw_newMediaObject', $id$args ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase
        
$attachment_id = (int) $attachment->ID;
    $file          = get_attached_file( $attachment_id );
    $meta          = wp_get_attachment_metadata( $attachment_id );

    if ( empty( $meta ) && file_exists( $file ) ) {
        $_meta = get_post_meta( $attachment_id );
        $_lock = 'wp_generating_att_' . $attachment_id;

        if ( ! array_key_exists( '_wp_attachment_metadata', $_meta ) && ! get_transient( $_lock ) ) {
            set_transient( $_lock$file );
            wp_update_attachment_metadata( $attachment_idwp_generate_attachment_metadata( $attachment_id$file ) );
            delete_transient( $_lock );
        }
    }
}

/** * Tries to convert an attachment URL into a post ID. * * @since 4.0.0 * * @global wpdb $wpdb WordPress database abstraction object. * * @param string $url The URL to resolve. * @return int The found post ID, or 0 on failure. */
Home | Imprint | This part of the site doesn't use cookies.