I’m doing a plugin that can retrieve media from external APIs, and when I click on it (on the admin), it insert a new post with the media, its caption, etc.
So, now, I’m inserting a new post on click : works fine. I did something like that :
$title = $_POST['title'];
$content = $_POST['content'];
$id = $_POST['id'];
$imgSrc = $_POST['imgSrc'];
$my_post = array(
'post_name' => $id,
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(8,39)
);
wp_insert_post( $my_post );
Now, that’s where I’m stuck. I have an image URL ($imgSrc
), that I want to add to my post. What I want to do is :
- upload this url in the media library : don’t know how to do that
- attach it to the post : I saw something with wp_insert_attachment (but I don’t know how to handle it)
As I’m pretty noob to PHP, how can I do it ?