Last active
October 17, 2025 00:50
-
-
Save m1r0/f22d5237ee93bcccb0d9 to your computer and use it in GitHub Desktop.
Revisions
-
m1r0 revised this gist
Feb 4, 2022 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,7 +7,7 @@ * @param int|null $parent_post_id The parent post ID (Optional). * @return int|false The attachment ID on success. False on failure. */ function wp_insert_attachment_from_url( $url, $parent_post_id = null ) { if ( ! class_exists( 'WP_Http' ) ) { require_once ABSPATH . WPINC . '/class-http.php'; -
m1r0 revised this gist
Jan 10, 2022 . 1 changed file with 23 additions and 22 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,33 +1,34 @@ <?php /** * Insert an attachment from a URL address. * * @param string $url The URL address. * @param int|null $parent_post_id The parent post ID (Optional). * @return int|false The attachment ID on success. False on failure. */ function my_insert_attachment_from_url( $url, $parent_post_id = null ) { if ( ! class_exists( 'WP_Http' ) ) { require_once ABSPATH . WPINC . '/class-http.php'; } $http = new WP_Http(); $response = $http->request( $url ); if ( 200 !== $response['response']['code'] ) { return false; } $upload = wp_upload_bits( basename( $url ), null, $response['body'] ); if ( ! empty( $upload['error'] ) ) { return false; } $file_path = $upload['file']; $file_name = basename( $file_path ); $file_type = wp_check_filetype( $file_name, null ); $attachment_title = sanitize_file_name( pathinfo( $file_name, PATHINFO_FILENAME ) ); $wp_upload_dir = wp_upload_dir(); $post_info = array( 'guid' => $wp_upload_dir['url'] . '/' . $file_name, @@ -37,18 +38,18 @@ function crb_insert_attachment_from_url($url, $parent_post_id = null) { 'post_status' => 'inherit', ); // Create the attachment. $attach_id = wp_insert_attachment( $post_info, $file_path, $parent_post_id ); // Include image.php. require_once ABSPATH . 'wp-admin/includes/image.php'; // Generate the attachment metadata. $attach_data = wp_generate_attachment_metadata( $attach_id, $file_path ); // Assign metadata to attachment. wp_update_attachment_metadata( $attach_id, $attach_data ); return $attach_id; } -
m1r0 renamed this gist
Aug 24, 2018 . 1 changed file with 12 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,12 +1,13 @@ <?php /** * Insert an attachment from an URL address. * * @param String $url * @param Int $parent_post_id * @return Int Attachment ID */ function crb_insert_attachment_from_url($url, $parent_post_id = null) { if( !class_exists( 'WP_Http' ) ) include_once( ABSPATH . WPINC . '/class-http.php' ); @@ -29,15 +30,15 @@ function crb_insert_attachment_from_url($url, $post_id = null) { $wp_upload_dir = wp_upload_dir(); $post_info = array( 'guid' => $wp_upload_dir['url'] . '/' . $file_name, 'post_mime_type' => $file_type['type'], 'post_title' => $attachment_title, 'post_content' => '', 'post_status' => 'inherit', ); // Create the attachment $attach_id = wp_insert_attachment( $post_info, $file_path, $parent_post_id ); // Include image.php require_once( ABSPATH . 'wp-admin/includes/image.php' ); @@ -50,4 +51,4 @@ function crb_insert_attachment_from_url($url, $post_id = null) { return $attach_id; } -
m1r0 revised this gist
Jun 5, 2015 . 1 changed file with 39 additions and 27 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,41 +1,53 @@ /** * Insert an attachment from an URL address. * * @param String $url * @param Int $post_id * @param Array $meta_data * @return Int Attachment ID */ function crb_insert_attachment_from_url($url, $post_id = null) { if( !class_exists( 'WP_Http' ) ) include_once( ABSPATH . WPINC . '/class-http.php' ); $http = new WP_Http(); $response = $http->request( $url ); if( $response['response']['code'] != 200 ) { return false; } $upload = wp_upload_bits( basename($url), null, $response['body'] ); if( !empty( $upload['error'] ) ) { return false; } $file_path = $upload['file']; $file_name = basename( $file_path ); $file_type = wp_check_filetype( $file_name, null ); $attachment_title = sanitize_file_name( pathinfo( $file_name, PATHINFO_FILENAME ) ); $wp_upload_dir = wp_upload_dir(); $post_info = array( 'guid' => $wp_upload_dir['url'] . '/' . $file_name, 'post_mime_type' => $file_type['type'], 'post_title' => $attachment_title, 'post_content' => '', 'post_status' => 'inherit', ); // Create the attachment $attach_id = wp_insert_attachment( $post_info, $file_path, $post_id ); // Include image.php require_once( ABSPATH . 'wp-admin/includes/image.php' ); // Define attachment metadata $attach_data = wp_generate_attachment_metadata( $attach_id, $file_path ); // Assign metadata to attachment wp_update_attachment_metadata( $attach_id, $attach_data ); return $attach_id; } -
m1r0 revised this gist
Sep 12, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ function crb_insert_attachment_from_url($image_url, $post_id = null) { $upload_dir = wp_upload_dir(); // Set upload folder $parse_url = parse_url($image_url); $image_data = file_get_contents('./.' . $parse_url['path']); // Get image data -
m1r0 created this gist
Sep 12, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,41 @@ function crb_insert_attachment_from_url($image_url, $post_id) { $upload_dir = wp_upload_dir(); // Set upload folder $parse_url = parse_url($image_url); $image_data = file_get_contents('./.' . $parse_url['path']); // Get image data $filename = basename($image_url); // Create image file name // Check folder permission and define file location if( wp_mkdir_p( $upload_dir['path'] ) ) { $file = $upload_dir['path'] . '/' . $filename; } else { $file = $upload_dir['basedir'] . '/' . $filename; } // Create the image file on the server file_put_contents( $file, $image_data ); // Check image file type $wp_filetype = wp_check_filetype( $filename, null ); // Set attachment data $attachment = array( 'post_mime_type' => $wp_filetype['type'], 'post_title' => sanitize_file_name( $filename ), 'post_content' => '', 'post_status' => 'inherit' ); // Create the attachment $attach_id = wp_insert_attachment( $attachment, $file, $post_id ); // Include image.php require_once(ABSPATH . 'wp-admin/includes/image.php'); // Define attachment metadata $attach_data = wp_generate_attachment_metadata( $attach_id, $file ); // Assign metadata to attachment wp_update_attachment_metadata( $attach_id, $attach_data ); return $attach_id; }