How to Upload Media From Url In WordPress

How to Upload Media From Url In WordPress

Please follow the snippet code for uploading media from url

<?php

            $image_url ='https://www.freelancedev.in/wp-content/uploads/2021/04/upload.png';
            $imagextension = end(explode('.',$image_url)) ;
            $image_name       = 'imagename.'.$imagextension;
            $upload_dir       = wp_upload_dir(); 
            $image_data       = file_get_contents($image_url); // Get image data
            $unique_file_name = wp_unique_filename( $upload_dir['path'], $image_name ); // Generate unique name
            $filename         = basename( $unique_file_name ); // Create image file name
            
            // Check folder permission and define file location
            if( wp_mkdir_p( $upload_dir['path'] ) ) {
                $file = $upload_dir['path'] . '/' . $filename;
            } else {
                $file = $upload_dir['basedir'] . '/' . $filename;
            }
            
            // Create the image  file on the server
            file_put_contents( $file, $image_data );
            
            // Check image file type
            $wp_filetype = wp_check_filetype( $filename, null );
            
            // Set attachment data
            $attachment = array(
                'post_mime_type' => $wp_filetype['type'],
                'post_title'     => sanitize_file_name( $filename ),
                'post_content'   => '',
                'post_status'    => 'inherit'
            );
            
            $attach_id = wp_insert_attachment( $attachment, $file, $post_id );
        
            $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
            
            // Assign metadata to attachment
            wp_update_attachment_metadata( $attach_id, $attach_data );

//setting media to post
            set_post_thumbnail( $post_id, $attach_id );

1 Comment

  • June 13, 2021

    Ahana Web Solutions

    Nice Blog

Leave A Reply