Skip to content

Instantly share code, notes, and snippets.

@andrewahead4
Last active April 18, 2024 13:55
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save andrewahead4/489e6422feb5be901143 to your computer and use it in GitHub Desktop.
Save andrewahead4/489e6422feb5be901143 to your computer and use it in GitHub Desktop.
A simple post insert using WP REST API and PHP over basic authentication
<?php
///////////////////////////////////////////////////////////////////////////////////
// //
// This is using a sample local WordPress Install and is not production safe //
// It uses the REST and Basic Auth plugins //
// //
///////////////////////////////////////////////////////////////////////////////////
// setup user name and password
$username = 'admin';
$password = 'password';
// the standard end point for posts in an initialised Curl
$process = curl_init('http://blogimport.dev/wp-json/wp/v2/posts');
// create an array of data to use, this is basic - see other examples for more complex inserts
$data = array('slug' => 'rest_insert' , 'title' => 'REST API insert' , 'content' => 'The content of our stuff', 'excerpt' => 'smaller' );
$data_string = json_encode($data);
// create the options starting with basic authentication
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
// make sure we are POSTing
curl_setopt($process, CURLOPT_CUSTOMREQUEST, "POST");
// this is the data to insert to create the post
curl_setopt($process, CURLOPT_POSTFIELDS, $data_string);
// allow us to use the returned data from the request
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
// we are sending json
curl_setopt($process, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
// process the request
$return = curl_exec($process);
curl_close($process);
// This buit is to show you on the screen what the data looks like returned and then decoded for PHP use
echo '<h2>Results</h2>';
print_r($return);
echo '<h2>Decoded</h2>';
$result = json_decode($return, true);
print_r($result);
@engnajjar
Copy link

the code is not working !!
is the any specific plugin required , please???

@ChrisWebbNZ
Copy link

Works fine, you just need https://github.com/wp-api/basic-auth installed and an appropriate user / password set up.

@engnajjar
Copy link

engnajjar commented Aug 3, 2018

thanks for replying :)
I've installed it and activate.
I've added a new user from wp dashboard and still the code doesn't work !!

@JohnDeeBDD
Copy link

Anyone know of a snippet that does this using auth cookies instead of basic auth?

@mccoyg
Copy link

mccoyg commented Sep 12, 2018

@alexdhika
Copy link

does not work for https site

@StevieDC
Copy link

Worked for me. As is. On an HTTPS site.
Thanks Andrew.

@Amarucevich
Copy link

Amarucevich commented Jul 15, 2021

Hello. thanks you. worked for me.
generate an application passwords and email credentials.
HTTPS
PHP 7.4.21 (cli) (built: Jul 2 2021 15:33:47) ( NTS )

Saludo, Andres.

@ayki
Copy link

ayki commented Aug 18, 2022

Thanks Andrew works like magic 👍

@DrLightman
Copy link

This the one of the few examples on the web to show how to actually interact with WordPress rest api with PHP and curl :)

If you add also the fields categories, tags, meta and featured_media it becomes PERFECT!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment