24

I really like the way SE uploads an image from a URL (I'm sure many do!). I've been searching, but can't find, is there a plugin or a method similar to this available for WordPress?

I know an image can be uploaded and crunched directly from a URL by entering the image URL into the File Name box after you click Upload/Insert Media >> From Computer >> Choose File

enter image description here

This is a great feature, but not very widely known (I actually just discovered it). I would like something a little more like SE, where there is an option that let the user know to add the image URL.

How can I go about adding simply the upload file field to a new tab in the media uploader?

Here is a tutorial for How to add a new tab in Media Upload page in wordpress, but I want to add only some text and the file upload field to that tab. Any ideas? I couldn't find anything in the WordPress Codex that deals with this feature or the file upload field directly.

Thanks.

5
  • Feature requests belong on trac.wordpress.org.
    – Wyck
    Commented Apr 24, 2012 at 23:10
  • 2
    Not a feature request. The feature is already built in. Commented Apr 24, 2012 at 23:31
  • @TravisPflanz Came across this for Windows and thought it was genius - definitely improved my workflow. Know of anyway to do that in Mac? command+shift+g doesn't seem to support urls, but wasn't sure if there was another command.
    – user658182
    Commented Nov 8, 2017 at 13:37
  • 1
    The advantage of upload from url over the "enter url in windows filemanager" is that the file is loaded directly from the source to the wordpress server; in my case over the hosting centers gigabit line instead of first being downloaded to my pc and then up to the wordpress over slow mobile connection.
    – Lenne
    Commented Jan 31, 2018 at 20:25
  • The trick to 'upload' directly from a URL does not work in Windows 10 (in any browser - tested Firefox, Chrome, IE11), and has probably not worked in previous versions of Windows since 2012. Windows will download the file from the URL to a temporary location on your computer and upload from there. So it is not possible to use this trick to 'upload' large video files (to bypass hosting provider's HTTP 413 response before PHP and WordPress even get a look-in).
    – Jake
    Commented Oct 31, 2019 at 23:12

4 Answers 4

29

you can write a php script, or make your own plugin of this code here, i used it in one of my projects where i had to import a large number of images.

first, get the image, and store it in your upload-directory:

$uploaddir = wp_upload_dir();
$uploadfile = $uploaddir['path'] . '/' . $filename;

$contents= file_get_contents('http://mydomain.com/folder/image.jpg');
$savefile = fopen($uploadfile, 'w');
fwrite($savefile, $contents);
fclose($savefile);

after that, we can insert the image into the media library:

$wp_filetype = wp_check_filetype(basename($filename), null );

$attachment = array(
    'post_mime_type' => $wp_filetype['type'],
    'post_title' => $filename,
    'post_content' => '',
    'post_status' => 'inherit'
);

$attach_id = wp_insert_attachment( $attachment, $uploadfile );

$imagenew = get_post( $attach_id );
$fullsizepath = get_attached_file( $imagenew->ID );
$attach_data = wp_generate_attachment_metadata( $attach_id, $fullsizepath );
wp_update_attachment_metadata( $attach_id, $attach_data );

and voila - here we go. you can also set various other parameters in the attachment array. if you got an array of urls or something like that, you can run the script in a loop - but be aware that the image functions take up a lot of time and memory to execute.

5
  • oh, i'm sorry, i didn't see the image at first. maybe i will write a small plugin doing exactly this. i hope you can get along with my script so far - i will keep you posted on plugin news.
    – fischi
    Commented Apr 30, 2012 at 17:07
  • I think I added the image just as you posted. I will take a more in-depth look when I get back from lunch. Thanks for your assistance! Always appreciated. Commented Apr 30, 2012 at 17:53
  • usage of file_get_contents with a URL will not work if allow_url_fopen is disabled in php.ini - wp_remote_get will be more highly compatible across different WP environments
    – highvolt
    Commented Feb 24, 2017 at 14:41
  • Hi, thanks for the answer, for what is wp_generate_attachment_metadata and wp_update_attachment_metadata ?
    – gdfgdfg
    Commented Jan 25, 2019 at 0:32
  • This is nice, but how do I get it to generate a Media Library thumbnail and multiple image sizes? Commented May 18, 2020 at 20:35
13

You can use the functions download_url() and wp_handle_sideload().

download_url()

Downloads a url to a local temporary file using the WordPress HTTP Class. Please note that the calling function must unlink() the file.

wp_handle_sideload()

Handle sideloads, which is the process of retrieving a media item from another server instead of a traditional media upload. This process involves sanitizing the filename, checking extensions for mime type, and moving the file to the appropriate directory within the uploads directory.

Example:

// Gives us access to the download_url() and wp_handle_sideload() functions
require_once( ABSPATH . 'wp-admin/includes/file.php' );

// URL to the WordPress logo
$url = 'http://s.w.org/style/images/wp-header-logo.png';
$timeout_seconds = 5;

// Download file to temp dir
$temp_file = download_url( $url, $timeout_seconds );

if ( !is_wp_error( $temp_file ) ) {

    // Array based on $_FILE as seen in PHP file uploads
    $file = array(
        'name'     => basename($url), // ex: wp-header-logo.png
        'type'     => 'image/png',
        'tmp_name' => $temp_file,
        'error'    => 0,
        'size'     => filesize($temp_file),
    );

    $overrides = array(
        // Tells WordPress to not look for the POST form
        // fields that would normally be present as
        // we downloaded the file from a remote server, so there
        // will be no form fields
        // Default is true
        'test_form' => false,

        // Setting this to false lets WordPress allow empty files, not recommended
        // Default is true
        'test_size' => true,
    );

    // Move the temporary file into the uploads directory
    $results = wp_handle_sideload( $file, $overrides );

    if ( !empty( $results['error'] ) ) {
        // Insert any error handling here
    } else {

        $filename  = $results['file']; // Full path to the file
        $local_url = $results['url'];  // URL to the file in the uploads dir
        $type      = $results['type']; // MIME type of the file

        // Perform any actions here based in the above results
    }

}
3
  • I Used this code and it successfully added the image in the uploads directory but when i go to my Media Library in the Wordpress backend I cant find it anywhere and it does not show up in the search. I made sure it had correct permissions but still not luck. Any reason it wouldn't be showing up?
    – Nick
    Commented Jun 14, 2018 at 23:05
  • It will not add entries in admin side. If you want to add entries in admin, then better you can use, wp_insert_attachment(); codex.wordpress.org/Function_Reference/wp_insert_attachment or you can use this by modifiying $_FILES global variable. media_handle_upload(); codex.wordpress.org/Function_Reference/media_handle_upload Commented Jun 20, 2018 at 14:31
  • it doesnt work at all
    – zEn feeLo
    Commented May 8, 2019 at 7:43
6

WordPress Plugin Directory - Grab & Save

This plugin allow you to grab image from remote url and save into your own wordpress media library. By doing so, you never worried if the remote image was removed by its owner. This also save you steps to download the image to local computer and upload again to your own wordpress.

After grabbing the image, wordpress will prompt you either to "insert into post" or "change attributes" just like after you upload an image.

1
2

There are at least three ways to import remote images into WordPress:

  1. Grab and Save Plugin, which is mentioned in the other answer. This plug-in is a bit older and it saves the file directly, so thumbnails in different sizes are not created. Last update over 2 years ago at the time of writing.

  2. Import External Image Plugin has bulk import for remote linked images. You may need to increase your PHP memory limit for this to work. Last update over 2 years ago at the time of writing.

  3. Save & Import Image from URL Plugin imports the image using native functions, so it is properly created in the media gallery and all thumbnails etc. are made. This plugin is last updated in 2016 and works with WordPress 4.7

Disclosure: I created the Save & Import Image from URL Plugin

1
  • thank you! Do you know about plugins compatible with current Wordpress versions (5.4)?
    – cduguet
    Commented Apr 10, 2020 at 10:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.