Skip to content

Instantly share code, notes, and snippets.

@maheshwaghmare
Created May 12, 2020 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maheshwaghmare/9e3e7806b345bebf9e3d84df64097cf1 to your computer and use it in GitHub Desktop.
Save maheshwaghmare/9e3e7806b345bebf9e3d84df64097cf1 to your computer and use it in GitHub Desktop.
Perform the "Basic Authentication" request in WordPress with plugin "Application Passwords". Read more at https://maheshwaghmare.com/doc/application-passwords/
<?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 = 'mK7M wZmN Fuj5 IKYF XUAv EZ8H';
$site_url = 'http://localhost/dev.test/';
$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