Save files or images from remote servers to WordPress Media Library

WordPress development process, sometimes we need to save the remote server files or images to the WordPress media library to achieve the function of caching or backup, the realization of the process is in fact the remote server to download the image, and then inserted into the media library.

Downloading files using the WordPress download_url function

WordPress provides us with a function to download the filedownload_url( $url )We can use this function to download a file from a remote server.

$tmp = download_url( $url );

Use WordPress media_handle_sideload function to save downloaded files to media library

download_url( $url )After downloading, the file is just saved as a temporary file, in this step, we need to save the temporary file to the media library. This step, we need to save the temporary file to the media library. Before saving, we need to get the file name of the file, if there is no requirement for the file name, randomly generate a file name is also possible. Then use the WordPressmedia_handle_sideloadFunction to save a file to the media library.

/**
 * Download the remote file to the WordPress server
 * The target remote file needs to be in a file format allowed by WordPress
 *
 * @param $url
 * @param $url
 * @return false|int|\WP_Error
 */
function download_remote_file($url){
    $tmp = download_url( $url );
    $file_array = array();

    // Set variables for saving files
    preg_match('/[^\?] +\. (jpg|jpe|jpeg|gif|png)/i', $url, $matches);
    $file_array['name'] = basename($matches[0]);
    $file_array['tmp_name'] = $tmp;

    // If saving the temporary file fails, delete the file
    if ( is_wp_error( $tmp ) ) {
        @unlink($file_array['tmp_name']);
        $file_array['tmp_name'] = '';
    }

    // Save the file to the media library
    $id = media_handle_sideload( $file_array, 0 );

    // If the save fails, delete the file
    if ( is_wp_error($id) ) {
        @unlink($file_array['tmp_name']);
        return $id.
    }

    return false; }
}

Pass the above function a remote URL and he will download that URL to the WordPress media library, insert it into the media database, and then return to us a media library ID, which we can use to do further processing.

Easier way media_sideload_image

The above function can save not only images on the remote server, but also other types of files, and if we just save files, there is an easier way:media_sideload_image, only the following lines of code are needed.

$url = "http://wordpress.org/about/images/logos/wordpress-logo-stacked-rgb.png";
$post_id = 1;
$desc = "The WordPress Logo";

$image = media_sideload_image($url, $post_id, $desc);

If you want to use it in the WordPress front page, you need to introduce the following files:

require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php'); require_once(ABSPATH .
require_once(ABSPATH . 'wp-admin/includes/image.php'); require_once(ABSPATH .;

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *