wp_generate_attachment_metadata example

'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;
    }

    

        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. */
$attachment['post_excerpt'] = $original_attachment->post_excerpt;
            }

            // Copy the image alt text attribute from the original image.             if ( '' !== trim( $original_attachment->_wp_attachment_image_alt ) ) {
                $attachment['meta_input'] = array(
                    '_wp_attachment_image_alt' => wp_slash( $original_attachment->_wp_attachment_image_alt ),
                );
            }

            $attachment_id = wp_insert_attachment( $attachment$cropped );
            $metadata      = wp_generate_attachment_metadata( $attachment_id$cropped );

            /** * Filters the cropped image attachment metadata. * * @since 4.3.0 * * @see wp_generate_attachment_metadata() * * @param array $metadata Attachment metadata. */
            $metadata = apply_filters( 'wp_ajax_cropped_attachment_metadata', $metadata );
            

                $image_attachment = apply_filters( 'attachment_thumbnail_args', $image_attachment$metadata$uploaded );

                $sub_attachment_id = wp_insert_attachment( $image_attachment$uploaded['file'] );
                add_post_meta( $sub_attachment_id, '_cover_hash', $hash );
                $attach_data = wp_generate_attachment_metadata( $sub_attachment_id$uploaded['file'] );
                wp_update_attachment_metadata( $sub_attachment_id$attach_data );
                update_post_meta( $attachment_id, '_thumbnail_id', $sub_attachment_id );
            }
        }
    } elseif ( 'application/pdf' === $mime_type ) {
        // Try to create image thumbnails for PDFs.
        $fallback_sizes = array(
            'thumbnail',
            'medium',
            'large',
        );
$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. */
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 */
/** * Inserts an attachment. * * @since 4.3.0 * * @param array $attachment An array with attachment object data. * @param string $file File path of the attached image. * @return int Attachment ID. */
    public function insert_attachment( $attachment$file ) {
        $attachment_id = wp_insert_attachment( $attachment$file );
        $metadata      = wp_generate_attachment_metadata( $attachment_id$file );

        /** * 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 );
        
$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
        
$attached_file = null;
                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 ) ) {
                        
$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.