// Set variables for storage, fix file filename for query strings.
preg_match( '/[^\?]+\.(' .
implode( '|',
$allowed_extensions ) . ')\b/i',
$file,
$matches );
if ( !
$matches ) { return new WP_Error( 'image_sideload_failed',
__( 'Invalid image URL.'
) );
} $file_array = array
();
$file_array['name'
] =
wp_basename( $matches[0
] );
// Download file to temp location.
$file_array['tmp_name'
] =
download_url( $file );
// If error storing temporarily, return the error.
if ( is_wp_error( $file_array['tmp_name'
] ) ) { return $file_array['tmp_name'
];
} // Do the validation and storage stuff.
$id =
media_handle_sideload( $file_array,
$post_id,
$desc );
// If error storing permanently, unlink.
if ( is_wp_error( $id ) ) { @