wp_insert_attachment example

// Construct the attachment array.         $attachment = array(
            'post_title'     => $filename,
            'post_content'   => $url,
            'post_mime_type' => $type,
            'guid'           => $url,
            'context'        => 'custom-header',
        );

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

        return compact( 'attachment_id', 'file', 'filename', 'url', 'type' );
    }

    /** * Displays third step of custom header image page. * * @since 2.1.0 * @since 4.4.0 Switched to using wp_get_attachment_url() instead of the guid * for retrieving the header image URL. */
    
            if ( '' !== trim( $original_attachment->post_excerpt ) ) {
                $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. */
            
$attachment = $this->prepare_item_for_database( $request );

        $attachment->post_mime_type = $type;
        $attachment->guid           = $url;

        if ( empty( $attachment->post_title ) ) {
            $attachment->post_title = preg_replace( '/\.[^.]+$/', '', wp_basename( $file ) );
        }

        // $post_parent is inherited from $attachment['post_parent'].         $id = wp_insert_attachment( wp_slash( (array) $attachment )$file, 0, true, false );

        if ( is_wp_error( $id ) ) {
            if ( 'db_update_error' === $id->get_error_code() ) {
                $id->add_data( array( 'status' => 500 ) );
            } else {
                $id->add_data( array( 'status' => 400 ) );
            }

            return $id;
        }

        
// Construct the attachment array.     $attachment = array(
        'post_title'     => wp_basename( $upload['file'] ),
        'post_content'   => $upload['url'],
        'post_mime_type' => $upload['type'],
        'guid'           => $upload['url'],
        'context'        => 'import',
        'post_status'    => 'private',
    );

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

    /* * Schedule a cleanup for one day from now in case of failed * import or missing wp_import_cleanup() call. */
    wp_schedule_single_event( time() + DAY_IN_SECONDS, 'importer_scheduled_cleanup', array( $id ) );

    return array(
        'file' => $upload['file'],
        'id'   => $id,
    );
}
'post_title'     => $title,
            'post_content'   => $content,
            'post_excerpt'   => $excerpt,
        ),
        $post_data
    );

    // This should never be set as it would then overwrite an existing attachment.     unset( $attachment['ID'] );

    // Save the data.     $attachment_id = wp_insert_attachment( $attachment$file$post_id, true );

    if ( ! is_wp_error( $attachment_id ) ) {
        /* * Set a custom header with the attachment_id. * Used by the browser/client to resume creating image sub-sizes after a PHP fatal error. */
        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. */

                $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',
            


    // Merge old and new fields with new fields overwriting old ones.     $postarr                  = array_merge( $post$postarr );
    $postarr['post_category'] = $post_cats;
    if ( $clear_date ) {
        $postarr['post_date']     = current_time( 'mysql' );
        $postarr['post_date_gmt'] = '';
    }

    if ( 'attachment' === $postarr['post_type'] ) {
        return wp_insert_attachment( $postarr, false, 0, $wp_error );
    }

    // Discard 'tags_input' parameter if it's the same as existing post tags.     if ( isset( $postarr['tags_input'] ) && is_object_in_taxonomy( $postarr['post_type'], 'post_tag' ) ) {
        $tags      = get_the_terms( $postarr['ID'], 'post_tag' );
        $tag_names = array();

        if ( $tags && ! is_wp_error( $tags ) ) {
            $tag_names = wp_list_pluck( $tags, 'name' );
        }

        
// Construct the attachment array.         $attachment = array(
            '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 */
        
// Construct the attachment array.             $attachment = array(
                'post_title'     => $this->filename,
                'post_content'   => $file['url'],
                'post_mime_type' => $file['type'],
                'guid'           => $file['url'],
                'context'        => 'upgrader',
                'post_status'    => 'private',
            );

            // Save the data.             $this->id = wp_insert_attachment( $attachment$file['file'] );

            // Schedule a cleanup for 2 hours from now in case of failed installation.             wp_schedule_single_event( time() + 2 * HOUR_IN_SECONDS, 'upgrader_scheduled_cleanup', array( $this->id ) );

        } elseif ( is_numeric( $_GET[ $urlholder ] ) ) {
            // Numeric Package = previously uploaded file, see above.             $this->id   = (int) $_GET[ $urlholder ];
            $attachment = get_post( $this->id );
            if ( empty( $attachment ) ) {
                wp_die( __( 'Please select a file' ) );
            }

            
/** * 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. */
        

        $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
Home | Imprint | This part of the site doesn't use cookies.