Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save maheshwaghmare/6817f61c08d6c4e560a25cccba57a292 to your computer and use it in GitHub Desktop.
Save maheshwaghmare/6817f61c08d6c4e560a25cccba57a292 to your computer and use it in GitHub Desktop.
Create post using wp_remote_post() function in WordPress. Required plugin https://github.com/WP-API/Basic-Auth
<?php
/**
* NOTE: Required plugin https://github.com/WP-API/Basic-Auth to install and activate on the same site.
* It works on same site.
*/
// API Details.
$rest_url = 'http://{MY_SITE}/wp-json/wp/v2/posts';
$username = 'my-username';
$password = 'my-password';
// API Request.
$response = wp_remote_post( $rest_url, array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( $username . ':' . $password ),
),
'body' => array(
// New post title & content.
'title' => 'New post title',
'content' => 'New post content goes here.'
)
) );
// Result.
$response_code = wp_remote_retrieve_response_message( $response );
// @DEBUG
// echo '<pre>';
// print_r($response);
// echo '</pre>';
@amirsa66
Copy link

Hi buddy,
Do you know how can I create multiple blog posts from several pairs of text and images (as post text and post image) in a different database?

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