I'm able to update a media with this code :
function createMedia($urlImage , $title)
{
global $usernameWP;
global $passwordWP;
$rest_api_url = "https://www.example.com/wp-json/wp/v2/media";
// Obtenir le contenu de l'image
$image_data = file_get_contents($urlImage);
// Créer un nom de fichier à partir de l'URL
$filename = basename($urlImage);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $rest_api_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $image_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: image/png', // Ajouter le type de contenu de l'image
'Content-Length: ' . strlen($image_data),
'Content-Disposition: attachment; filename=' . $filename, // En-tête Content-Disposition
'Authorization: Basic ' . base64_encode($usernameWP . ':' . $passwordWP),
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$resultJsonString = curl_exec($ch);
print_r($resultJsonString);
$resultJsonObject = null;
if ($resultJsonString)
{
$resultJsonObject = json_decode($resultJsonString);
}
else
{
logMessage("createPostWPWithRestAPI : no result");
}
curl_close($ch);
return $resultJsonObject;
}
But how to specify some field like alt_text, caption?
It will be more simple to send json_data and specify the url image in a field...
Stackoverflow told me there is too much code in my question so I add blablablaStackoverflow told me there is too much code in my question so I add blablabla Stackoverflow told me there is too much code in my question so I add blablabla
Thanks
If you use WordPress' REST API for media Media, 'alt_text' and 'caption' are parameters that you can pass along to have them included in your updated Media, see details here in WordPress reference doc: https://developer.wordpress.org/rest-api/reference/media/
This is not tested, but I think this could be achieved by setting curl_setopt to true and then use CURLOPT_POSTFIELDS to pass the parameters, like so: