Create new posts on Wordpress Site via REST API using PHP Code

appvipo dot comappvipo dot com
1 min read

<?php

// API URL for creating a new post

$url = 'https://your_domain.com/wp-json/wp/v2/posts';

// Data for the new post

$data = [ 'title' => 'Hello World', 'status' => 'publish', 'content' => 'This is the post content.', 'excerpt' => 'This is a short excerpt.', ];

// Encode data to JSON

$data_string = json_encode($data);

// Setup the cURL session

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");

curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Authorization: Basic ' . base64_encode('your_username:your_application_password'), 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string) ]);

// Execute the request

$result = curl_exec($ch);

curl_close($ch);

// Output the results

echo $result; ?>

We used the above code for the purpose of creating articles on our appvipo page!

1
Subscribe to my newsletter

Read articles from appvipo dot com directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

appvipo dot com
appvipo dot com

Appvipo is a platform that provides apk and mod apk of top apps and games, completely free. In addition to that, Appvipo is also a reliable source for the latest technology information, updated daily!