src/class-wp-image.php
public static function save( $url, $post_ID, $featured = false ) {

		$url = filter_var( $url, FILTER_VALIDATE_URL );

		if ( false === $url || false === get_post_status( $post_ID ) ) {
			return false;
		}

		$filename = basename( $url );
		$filepath = self::upload( $url, $filename );

		$attachment = [
			'post_mime_type' => wp_check_filetype( $filename, null )['type'],
			'post_title'     => sanitize_file_name( $filename ),
			'post_content'   => '',
			'post_status'    => 'inherit',
		];

		$attach_id = wp_insert_attachment( $attachment, $filepath, $post_ID );

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

		$attach_data = wp_generate_attachment_metadata( $attach_id, $filepath );

		wp_update_attachment_metadata( $attach_id, $attach_data );

		if ( $featured ) {
			set_post_thumbnail( $post_ID, $attach_id );
		}

		return wp_get_attachment_url( $attach_id );
	}
public static function save( $url, $post_ID, $featured = false ) {

		$url = filter_var( $url, FILTER_VALIDATE_URL );

		if ( false === $url || false === get_post_status( $post_ID ) ) {
			return false;
		}

		$filename = basename( $url );
		$filepath = self::upload( $url, $filename );

		$attachment = [
			'post_mime_type' => wp_check_filetype( $filename, null )['type'],
			'post_title'     => sanitize_file_name( $filename ),
			'post_content'   => '',
			'post_status'    => 'inherit',
		];

		$attach_id = wp_insert_attachment( $attachment, $filepath, $post_ID );

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

		$attach_data = wp_generate_attachment_metadata( $attach_id, $filepath );

		wp_update_attachment_metadata( $attach_id, $attach_data );

		if ( $featured ) {
			set_post_thumbnail( $post_ID, $attach_id );
		}

		return wp_get_attachment_url( $attach_id );
	}
public static function save ( $ url , $ post_ID , $ featured = false ) { $ url = filter_var ( $ url , FILTER_VALIDATE_URL ) ; if ( false === $ url || false === get_post_status ( $ post_ID ) ) { return false ; } $ filename = basename ( $ url ) ; $ filepath = self :: upload ( $ url , $ filename ) ; $ attachment = [ 'post_mime_type' => wp_check_filetype ( $ filename , null ) [ 'type' ] , 'post_title' => sanitize_file_name ( $ filename ) , 'post_content' => '' , 'post_status' => 'inherit' , ] ; $ attach_id = wp_insert_attachment ( $ attachment , $ filepath , $ post_ID ) ; require_once ( ABSPATH . 'wp-admin/includes/image.php' ) ; $ attach_data = wp_generate_attachment_metadata ( $ attach_id , $ filepath ) ; wp_update_attachment_metadata ( $ attach_id , $ attach_data ) ; if ( $ featured ) { set_post_thumbnail ( $ post_ID , $ attach_id ) ; } return wp_get_attachment_url ( $ attach_id ) ; }
Save image and associate it with a specific post.

@param string  $url      → external url image.
@param int     $post_ID  → post id.
@param boolean $featured → if image is featured.

@return string|false → URI for an attachment file or false on failure.
Save image and associate it with a specific post .