Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Perform the "Basic Authentication" request in WordPress with plugin "JSON Basic Authentication". Read more at https://maheshwaghmare.com/doc/basic-authentication/
<?php
if( ! function_exists( 'prefix_create_new_post' ) ) :
/**
* Create new post with wp_remote_post()
*
* @since 1.0.0
* @return void
*/
function prefix_create_new_post() {
$username = 'admin';
$password = 'admin';
$site_url = 'http://localhost/maheshwaghmare.com/';
$request = wp_remote_post( $site_url . 'wp-json/wp/v2/posts/', array(
'body' => array(
'title' => 'Rest API Post 3',
),
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( $username . ':' . $password ),
),
) );
}
add_action( 'admin_head', 'prefix_create_new_post' );
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment