Last active
April 4, 2021 06:23
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?