Last active
July 22, 2020 21:13
-
-
Save zaheer-bashir/aaddecd6743eaee5d2dd4e84ef2d80ae to your computer and use it in GitHub Desktop.
Revisions
-
zaheer-bashir revised this gist
Jul 22, 2020 . 1 changed file with 5 additions 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,10 +1,14 @@ /**** **** Add Feature Image ****/ $url = "apple.jpg"; $image_url = $url; $url_array = explode('/', $url ); $image_name = $url_array[count($url_array)-1]; $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $image_url); // Getting binary data -
zaheer-bashir revised this gist
Jul 22, 2020 . No changes.There are no files selected for viewing
-
zaheer-bashir renamed this gist
Jul 22, 2020 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
zaheer-bashir created this gist
Jul 22, 2020 .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,58 @@ // Add Feature Image $url = "apple.jpg"; $image_url = $url; $url_array = explode('/', $url ); $image_name = $url_array[count($url_array)-1]; $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $image_url); // Getting binary data curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); $image_data = curl_exec($ch); curl_close($ch); $upload_dir = wp_upload_dir(); // Set upload folder $unique_file_name = wp_unique_filename( $upload_dir['path'], $image_name ); // Generate unique name $filename = basename( $unique_file_name ); // 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 ); // asign to feature image // And finally assign featured image to post set_post_thumbnail( $post_id, $attach_id );