PHP 7.3.7 – Guardar fecha formateada en una variable

Para guardar una fecha formateada en una variable, escribimos:

setlocale(LC_TIME, 'es_VE', 'es_VE.utf-8', 'es_VE.utf8'); 
date_default_timezone_set('America/Caracas');
			
$fechaObjeto = Time::now();
		
$fechaFormateada = date_format($fechaObjeto, "Y-m-d");
echo "
fecha formateada: " . $fechaFormateada;

Php – cURL y CRUD usuarios con Wp REST API v2

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' => 'pedro@gmail.com', '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);
    
  • Read:
  • // 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);
    
  • Update:
  • // Php Update
    $host = 'https://dapliw.org.ve/wp-json/wp/v2/users/2';
    $data = array('email' => 'nuevoemail@gmail.com');
    $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);  
    
  • Delete:
  • // 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);
    
  • En la operación Delete se deben pasar en la variable $data los parámetros: ‘force’ => true y ‘reassign’ => idDelUsuarioAlCualSeLeAsignaranLosPostDelUsuarioQueSePretendeEliminar

Fuente: https://decodecms.com/como-usar-la-rest-api-de-wordpress/