How to specify some fields like "alt" or "caption" when creating a media with WP rest API

26 Views Asked by At

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

1

There are 1 best solutions below

5
Tami On

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:

function createMediaWithCaptionAlt($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);
    //New Code Here
    $parameters = [
       'image' => $image_data,
       'caption' => __('A translateable string caption', 'your-text-domain'),
       'alt_text' => __('A translateable Alt Text', 'your-text-domain'),
    ];
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);

    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;

}