admin管理员组文章数量:1426047
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
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.
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
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.
Share Improve this question edited May 3, 2012 at 11:45 Stephen Harris 32.6k6 gold badges84 silver badges118 bronze badges asked Apr 24, 2012 at 20:55 Travis PflanzTravis Pflanz 1,9535 gold badges31 silver badges57 bronze badges 5- Feature requests belong on trac.wordpress. – Wyck Commented Apr 24, 2012 at 23:10
- 2 Not a feature request. The feature is already built in. – Travis Pflanz 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
Reset to default 29you 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/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.
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/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
}
}
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.
There are at least three ways to import remote images into WordPress:
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.
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.
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
本文标签: Image Upload from URL
版权声明:本文标题:Image Upload from URL 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745370282a2655701.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论