Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save uiliw/0e0e003b33a14c1fb2277670c749abd4 to your computer and use it in GitHub Desktop.
Save uiliw/0e0e003b33a14c1fb2277670c749abd4 to your computer and use it in GitHub Desktop.
function external_images($url_external = null)
{
// 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 = $url_external;
$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
// Check the type of file. We'll use this as the 'post_mime_type'.
$filetype = wp_check_filetype( basename( $filename ), null );
// Get the path to the upload directory.
$wp_upload_dir = wp_upload_dir();
// Prepare an array of post data for the attachment.
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
);
//HOWTO: get the current created/updated post?
$post_id = null;
//if image exist update else create it
if (post_exists(basename( $filename ))){
$page = get_page_by_title(basename( $filename ), OBJECT, 'attachment');
$attach_id = $page->ID;
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename ); // Generate attachment data, filesize, height, width etc.
wp_update_attachment_metadata( $attach_id, $attach_data ); // Add the above meta data
return $local_url;
}
else{
// Insert the attachment.
$attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $post_id, $attach_id );
// Perform any actions here based in the above results
return $local_url;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment