Skip to content

Instantly share code, notes, and snippets.

@m1r0
Last active October 17, 2025 00:50
Show Gist options
  • Save m1r0/f22d5237ee93bcccb0d9 to your computer and use it in GitHub Desktop.
Save m1r0/f22d5237ee93bcccb0d9 to your computer and use it in GitHub Desktop.

Revisions

  1. m1r0 revised this gist Feb 4, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wp_insert_attachment_from_url.php
    Original 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 my_insert_attachment_from_url( $url, $parent_post_id = null ) {
    function wp_insert_attachment_from_url( $url, $parent_post_id = null ) {

    if ( ! class_exists( 'WP_Http' ) ) {
    require_once ABSPATH . WPINC . '/class-http.php';
  2. m1r0 revised this gist Jan 10, 2022. 1 changed file with 23 additions and 22 deletions.
    45 changes: 23 additions & 22 deletions wp_insert_attachment_from_url.php
    Original file line number Diff line number Diff line change
    @@ -1,33 +1,34 @@
    <?php

    /**
    * Insert an attachment from an URL address.
    * Insert an attachment from a URL address.
    *
    * @param String $url
    * @param Int $parent_post_id
    * @return Int Attachment ID
    * @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 crb_insert_attachment_from_url($url, $parent_post_id = null) {
    function my_insert_attachment_from_url( $url, $parent_post_id = null ) {

    if( !class_exists( 'WP_Http' ) )
    include_once( ABSPATH . WPINC . '/class-http.php' );
    if ( ! class_exists( 'WP_Http' ) ) {
    require_once ABSPATH . WPINC . '/class-http.php';
    }

    $http = new WP_Http();
    $http = new WP_Http();
    $response = $http->request( $url );
    if( $response['response']['code'] != 200 ) {
    if ( 200 !== $response['response']['code'] ) {
    return false;
    }

    $upload = wp_upload_bits( basename($url), null, $response['body'] );
    if( !empty( $upload['error'] ) ) {
    $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 );
    $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();
    $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
    // 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' );
    // Include image.php.
    require_once ABSPATH . 'wp-admin/includes/image.php';

    // Define attachment metadata
    // 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 );
    // Assign metadata to attachment.
    wp_update_attachment_metadata( $attach_id, $attach_data );

    return $attach_id;

    }
    }
  3. m1r0 renamed this gist Aug 24, 2018. 1 changed file with 12 additions and 11 deletions.
    23 changes: 12 additions & 11 deletions gistfile1.php → wp_insert_attachment_from_url.php
    Original 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 $post_id
    * @param Array $meta_data
    * @param String $url
    * @param Int $parent_post_id
    * @return Int Attachment ID
    */
    function crb_insert_attachment_from_url($url, $post_id = null) {
    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',
    '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 );
    $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;

    }
    }
  4. m1r0 revised this gist Jun 5, 2015. 1 changed file with 39 additions and 27 deletions.
    66 changes: 39 additions & 27 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -1,41 +1,53 @@
    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
    $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;
    /**
    * 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;
    }

    // Create the image file on the server
    file_put_contents( $file, $image_data );

    // Check image file type
    $wp_filetype = wp_check_filetype( $filename, null );
    $upload = wp_upload_bits( basename($url), null, $response['body'] );
    if( !empty( $upload['error'] ) ) {
    return false;
    }

    // Set attachment data
    $attachment = array(
    'post_mime_type' => $wp_filetype['type'],
    'post_title' => sanitize_file_name( $filename ),
    'post_content' => '',
    'post_status' => 'inherit'
    $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( $attachment, $file, $post_id );
    $attach_id = wp_insert_attachment( $post_info, $file_path, $post_id );

    // Include image.php
    require_once(ABSPATH . 'wp-admin/includes/image.php');
    require_once( ABSPATH . 'wp-admin/includes/image.php' );

    // Define attachment metadata
    $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
    $attach_data = wp_generate_attachment_metadata( $attach_id, $file_path );

    // Assign metadata to attachment
    wp_update_attachment_metadata( $attach_id, $attach_data );
    wp_update_attachment_metadata( $attach_id, $attach_data );

    return $attach_id;
    }

    }
  5. m1r0 revised this gist Sep 12, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    function crb_insert_attachment_from_url($image_url, $post_id) {
    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
  6. m1r0 created this gist Sep 12, 2014.
    41 changes: 41 additions & 0 deletions gistfile1.php
    Original 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;
    }