Last active
October 4, 2018 11:40
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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