Skip to content

Instantly share code, notes, and snippets.

@man4toman
Last active October 4, 2018 11:40
Show Gist options
  • Save man4toman/1545408b8965e812f0d30e39ce399f9a to your computer and use it in GitHub Desktop.
Save man4toman/1545408b8965e812f0d30e39ce399f9a to your computer and use it in GitHub Desktop.
Download an image from the URL and attach it to a post in WordPress
<?php
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
if(!function_exists('wp_get_current_user')) {
include(ABSPATH . "wp-includes/pluggable.php");
}
$url = "http://xyz.ltd/filenam.png";//Just a sample
$file = array();
$file['name'] = $url;
$file['tmp_name'] = download_url($url);
media_handle_sideload( $file, $post_id );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment