WordPress REST API – Upload featured Image


Read more: WordPress REST API – Create, Update or Delete posts using Basic Auth and HTTP API

Step 1: Upload media
CURL:

curl -sk --request POST --url http://tutorialspots.com/wp-json/wp/v2/media --header "cache-control: no-cache" --header "content-disposition: attachment; filename=file.png" --header "authorization: Basic $base64credentials" --header "content-type: image/png" --data-binary "@/fullpath/file.png" --location

PHP

<?php

function uploadMedia($url,$localfile)
{    
    $file = file_get_contents($localfile);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);     
    curl_setopt($ch, CURLOPT_TIMEOUT,30);     
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $uaa = 'Mozilla/5.0 (Windows; U; Windows NT 6.0; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3'; 
    curl_setopt($ch, CURLOPT_USERAGENT, $uaa);
    
    $filename = explode("/",$localfile);
    $filename = array_pop($filename);
    
    $filetype = mime_content_type($localfile);
    
    $header = array(
        "authorization: Basic ".base64_encode( 'USER:PASSWORD' ),        
        "cache-control: no-cache",
        "content-disposition: attachment; filename=$filename",
        "content-type: $filetype",
    );
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $file);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
     
    return curl_exec($ch);
}

$x = uploadMedia('http://tutorialspots.com/wp-json/wp/v2/media', __DIR__."/demo.jpg");

var_dump($x);

Step 2: Add data for media
CURL:

curl -sk --request POST --url http://tutorialspots.com/wp-json/wp/v2/media/$id --header "authorization: Basic $base64credentials " --header "content-type: application/json" -d '{"title":"$title", "caption":"$caption", "description":"$description", "alt_text":"$alttext", "date":"$date", "slug":"$slug", "post":"$idpost", "status":"publish" }'

PHP:

function grabAPI($url,$method='POST',$data='')
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);     
    curl_setopt($ch, CURLOPT_TIMEOUT,30);     
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $uaa = 'Mozilla/5.0 (Windows; U; Windows NT 6.0; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3'; 
    curl_setopt($ch, CURLOPT_USERAGENT, $uaa);
    $header = array(
        'authorization: Basic '.base64_encode( 'USER:PASSWORD' ),
        'content-type: application/json',
    );
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST,$method);
    if($data)
        curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
    
    return curl_exec($ch);
}

$title = 'demo image';
$slug = 'demo-image';

$x = grabAPI('http://tutorialspots.com/wp-json/wp/v2/media/6426','POST','{"title":"'.$title.'", "caption":"'.$title.'", "description":"'.$title.'", "alt_text":"'.$title.'", "date":"2020-06-01T03:35:31", "slug":"'.$slug.'", "post":"6425", "status":"publish"}');

var_dump($x);

Step 3: Assign featured image for post
CURL:

curl -sk --request POST --url http://tutorialspots.com/wp-json/wp/v2/posts/$postid --header "authorization: Basic $base64credentials " --header "content-type: application/json" -d '{"featured_media":"$mediaid"}'

PHP:

$x = grabAPI('http://tutorialspots.com/wp-json/wp/v2/posts/6425','POST','{"featured_media":"6426"}');

var_dump($x);

Recent search terms:

  • free upload image api

Leave a Reply