Title: wp_insert_attachment()?
Last modified: September 1, 2016

---

# wp_insert_attachment()?

 *  [MACscr](https://wordpress.org/support/users/macscr/)
 * (@macscr)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/wp_insert_attachment-1/)
 * Im inserting images using wp_insert_attachment(). Is there a way I can use your
   plugin to replace files? Most of my media is being inserted through this function.
   I can replace it with yours if it works as wrapper. My code looks like so:
 *     ```
       /**
        * 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' );
   
       	if(empty($url)) {
       		return false;
       	}
   
       	$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;
   
       }
       ```
   
 * [https://wordpress.org/plugins/enable-media-replace/](https://wordpress.org/plugins/enable-media-replace/)

The topic ‘wp_insert_attachment()?’ is closed to new replies.

 * ![](https://ps.w.org/enable-media-replace/assets/icon-256x256.png?rev=1940728)
 * [Enable Media Replace](https://wordpress.org/plugins/enable-media-replace/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/enable-media-replace/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/enable-media-replace/)
 * [Active Topics](https://wordpress.org/support/plugin/enable-media-replace/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/enable-media-replace/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/enable-media-replace/reviews/)

 * 0 replies
 * 1 participant
 * Last reply from: [MACscr](https://wordpress.org/support/users/macscr/)
 * Last activity: [9 years, 9 months ago](https://wordpress.org/support/topic/wp_insert_attachment-1/)
 * Status: not resolved