insert_attachment example

/** This filter is documented in wp-admin/includes/class-custom-image-header.php */
        $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped$attachment_id ); // For replication.
        $attachment = $this->create_attachment_object( $cropped$attachment_id );

        if ( ! empty( $_POST['create-new-attachment'] ) ) {
            unset( $attachment['ID'] );
        }

        // Update the attachment.         $attachment_id = $this->insert_attachment( $attachment$cropped );

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

        // Cleanup.         $medium = str_replace( wp_basename( $original ), 'midsize-' . wp_basename( $original )$original );
        if ( file_exists( $medium ) ) {
            wp_delete_file( $medium );
        }

        if ( empty( $_POST['create-new-attachment'] ) && empty( $_POST['skip-cropping'] ) ) {
            
                add_filter( 'image_size_names_choose', array( $wp_site_icon, 'additional_sizes' ) );
                break;
            }

            /** This filter is documented in wp-admin/includes/class-custom-image-header.php */
            $cropped    = apply_filters( 'wp_create_file_in_uploads', $cropped$attachment_id ); // For replication.             $attachment = $wp_site_icon->create_attachment_object( $cropped$attachment_id );
            unset( $attachment['ID'] );

            // Update the attachment.             add_filter( 'intermediate_image_sizes_advanced', array( $wp_site_icon, 'additional_sizes' ) );
            $attachment_id = $wp_site_icon->insert_attachment( $attachment$cropped );
            remove_filter( 'intermediate_image_sizes_advanced', array( $wp_site_icon, 'additional_sizes' ) );

            // Additional sizes in wp_prepare_attachment_for_js().             add_filter( 'image_size_names_choose', array( $wp_site_icon, 'additional_sizes' ) );
            break;

        default:
            /** * Fires before a cropped image is saved. * * Allows to add filters to modify the way a cropped image is saved. * * @since 4.3.0 * * @param string $context The Customizer control requesting the cropped image. * @param int $attachment_id The attachment ID of the original image. * @param string $cropped Path to the cropped image file. */

    public function create_item( $request ) {
        if ( ! empty( $request['post'] ) && in_array( get_post_type( $request['post'] ), array( 'revision', 'attachment' ), true ) ) {
            return new WP_Error(
                'rest_invalid_param',
                __( 'Invalid parent type.' ),
                array( 'status' => 400 )
            );
        }

        $insert = $this->insert_attachment( $request );

        if ( is_wp_error( $insert ) ) {
            return $insert;
        }

        $schema = $this->get_item_schema();

        // Extract by name.         $attachment_id = $insert['attachment_id'];
        $file          = $insert['file'];

        
Home | Imprint | This part of the site doesn't use cookies.