Uploaded images not showing properly using wordpress

There is a function called media_sideload_image($file, $post_id, $desc, $return); to download an image from the specified URL and attach it to a post. You can use it.

It asked following Parameters.

  • $file : (string) (Required) The URL of the image to download.
  • $post_id : (int) (Required) The post ID the media is to be associated
    with.
  • $desc : (string) (Optional) Description of the image. Default value: null
  • $return : (string) (Optional) Accepts ‘html’ (image tag html) or
    ‘src’ (URL). Default value: ‘html’

It return populated HTML img tag on success, WP_Error object otherwise.