20

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.

  • Feature requests belong on trac.wordpress.org. – Wyck Apr 24 '12 at 23:10
  • 2
    Not a feature request. The feature is already built in. – Travis Pflanz Apr 24 '12 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 Nov 8 '17 at 13:37
  • 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 Jan 31 '18 at 20:25
22

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.

  • 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 Apr 30 '12 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. – Travis Pflanz Apr 30 '12 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 Feb 24 '17 at 14:41
  • Hi, thanks for the answer, for what is wp_generate_attachment_metadata and wp_update_attachment_metadata ? – gdfgdfg Jan 25 at 0:32
11

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
    }

}
  • 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 Jun 14 '18 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 – Rajilesh Panoli Jun 20 '18 at 14:31
  • it doesnt work at all – Cameron A May 8 at 7:43
5

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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