Skip to content

Instantly share code, notes, and snippets.

@ajskelton
Last active March 15, 2022 04:47
Show Gist options
  • Save ajskelton/23d4dda9e3b837f408e20b5dc23f6b52 to your computer and use it in GitHub Desktop.
Save ajskelton/23d4dda9e3b837f408e20b5dc23f6b52 to your computer and use it in GitHub Desktop.

Revisions

  1. ajskelton revised this gist Feb 10, 2020. 1 changed file with 50 additions and 50 deletions.
    100 changes: 50 additions & 50 deletions wp-external-image-handler.php
    Original file line number Diff line number Diff line change
    @@ -1,51 +1,51 @@
    /**
    * Uploads an image from remote url and sets as featured image of the new post
    *
    * @param int $post_id The id of the new post
    * @param string $thumbnail_url Url of the preview image hosted by BrightTalk
    */
    private function image_handler( $post_id, $thumbnail_url ) {
    $image_url = $thumbnail_url; // Define the image URL here
    $image_name = basename( $thumbnail_url );
    $upload_dir = wp_upload_dir(); // Set upload folder
    $image_data = file_get_contents( $image_url ); // Get image data
    $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 );
    // And finally assign featured image to post
    set_post_thumbnail( $post_id, $attach_id );
    }
    * Uploads an image from remote url and sets as featured image of the new post
    *
    * @param int $post_id The id of the new post
    * @param string $thumbnail_url Url of the preview image hosted by BrightTalk
    */
    private function image_handler( $post_id, $thumbnail_url ) {
    $image_url = $thumbnail_url; // Define the image URL here
    $image_name = basename( $thumbnail_url );
    $upload_dir = wp_upload_dir(); // Set upload folder
    $image_data = file_get_contents( $image_url ); // Get image data
    $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 );
    // And finally assign featured image to post
    set_post_thumbnail( $post_id, $attach_id );
    }
  2. ajskelton created this gist Feb 10, 2020.
    51 changes: 51 additions & 0 deletions wp-external-image-handler.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    /**
    * Uploads an image from remote url and sets as featured image of the new post
    *
    * @param int $post_id The id of the new post
    * @param string $thumbnail_url Url of the preview image hosted by BrightTalk
    */
    private function image_handler( $post_id, $thumbnail_url ) {

    $image_url = $thumbnail_url; // Define the image URL here
    $image_name = basename( $thumbnail_url );
    $upload_dir = wp_upload_dir(); // Set upload folder
    $image_data = file_get_contents( $image_url ); // Get image data
    $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 );

    // And finally assign featured image to post
    set_post_thumbnail( $post_id, $attach_id );
    }