Para crear, leer, actualizar y eliminar usuarios (Ccreate, Read, Uptate y Delete) de WordPress utilizando la WP REST API v2 desde un programa cliente Php, seguimos los siguientes pasos:
- Create:
// Php Create $host = 'https://miDominio/wp-json/wp/v2/users'; $data = array('username' => 'pedroperez', 'password' => 'pedro123', 'email' => '[email protected]', 'first_name' => 'Pedro', 'last_name' => 'Pérez'); $data_string = json_encode($data); $headers = array( 'Content-Type:application/json', 'Content-Length: ' . strlen($data_string), 'Authorization: Basic '. base64_encode('miUsuarioWordpress:miClaveWordpress') ); $ch = curl_init($host); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); $result = curl_exec($ch); curl_close($ch); echo($result);
// Php Read $host = 'https://miDominio/wp-json/wp/v2/users/2'; $headers = array( 'Content-Type:application/json', 'Authorization: Basic '. base64_encode('miUsuarioWordpress:miClaveWordpress') ); $ch = curl_init($host); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); $result = curl_exec($ch); curl_close($ch); echo($result);
// Php Update $host = 'https://dapliw.org.ve/wp-json/wp/v2/users/2'; $data = array('email' => '[email protected]'); $data_string = json_encode($data); $headers = array( 'Content-Type:application/json', 'Content-Length: ' . strlen($data_string), 'Authorization: Basic '. base64_encode('miUsuarioWordpress:miClaveWordpress') ); $ch = curl_init($host); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); $result = curl_exec($ch); curl_close($ch); echo($result);
// Php Delete $host = 'https://dapliw.org.ve/wp-json/wp/v2/users/2'; $data = array('force' => true, 'reassign' => 1); $data_string = json_encode($data); $headers = array( 'Content-Type:application/json', 'Content-Length: ' . strlen($data_string), 'Authorization: Basic '. base64_encode('miUsuarioWordpress:miClaveWordpress') ); $ch = curl_init($host); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); $result = curl_exec($ch); curl_close($ch); echo($result);
Fuente: https://decodecms.com/como-usar-la-rest-api-de-wordpress/