I want to upload a file from external URL to the WordPress Media Library (that is common to ALL POSTS/PAGES). The return value I want to get is attachment ID in order to inject them into a shortcode (e.g. img).
I tried using IMEDIA_HANDLE_SIDELOAD but I got lost with $_FILES settings.
However, I am not sure about the parameters:
- Is this the right function?
- where in the code (aFile) should I place the URL I want to download from?
- What is the “tmp_name” and “name”?
See my code:
// my params
$post_id = 0; // is this what makes it common to all posts/pages?
$url = 'www.some_external_url.com/blabla.png';
// example from some forum
$filepath = '/relative/path/to/file.jpg';
$wp_filetype = wp_check_filetype( basename( $filepath ), null );
$aFile["name"] = basename( $filepath );
$aFile["type"] = $wp_filetype;
$afile["tmp_name"] = $filepath;
$attach_id = $media_handle_sideload( $aFile, $post_id, 'sometitle' );
Solution: