media_handle_sideload example

if ( empty( $file_array['tmp_name'] ) ) {
                        continue;
                    }

                    $attachment_post_data = array_merge(
                        wp_array_slice_assoc( $attachment, array( 'post_title', 'post_content', 'post_excerpt' ) ),
                        array(
                            'post_status' => 'auto-draft', // So attachment will be garbage collected in a week if changeset is never published.                         )
                    );

                    $attachment_id = media_handle_sideload( $file_array, 0, null, $attachment_post_data );
                    if ( is_wp_error( $attachment_id ) ) {
                        continue;
                    }
                    update_post_meta( $attachment_id, '_starter_content_theme', $this->get_stylesheet() );
                    update_post_meta( $attachment_id, '_customize_draft_post_name', $attachment['post_name'] );
                }

                $attachment_ids[ $symbol ] = $attachment_id;
            }
            $starter_content_auto_draft_post_ids = array_merge( $starter_content_auto_draft_post_idsarray_values( $attachment_ids ) );
        }

        
$file_array['name'] = wp_basename( $matches[0] );

        // Download file to temp location.         $file_array['tmp_name'] = download_url( $file );

        // If error storing temporarily, return the error.         if ( is_wp_error( $file_array['tmp_name'] ) ) {
            return $file_array['tmp_name'];
        }

        // Do the validation and storage stuff.         $id = media_handle_sideload( $file_array$post_id$desc );

        // If error storing permanently, unlink.         if ( is_wp_error( $id ) ) {
            @unlink( $file_array['tmp_name'] );
            return $id;
        }

        // Store the original attachment source in meta.         add_post_meta( $id, '_source_url', $file );

        // If attachment ID was requested, return it.
Home | Imprint | This part of the site doesn't use cookies.