Skip to content

Instantly share code, notes, and snippets.

@dasbairagya
Last active July 24, 2021 09:57
Show Gist options
  • Save dasbairagya/c335eaa94a14c36909b923dc86424aff to your computer and use it in GitHub Desktop.
Save dasbairagya/c335eaa94a14c36909b923dc86424aff to your computer and use it in GitHub Desktop.
Basic authentication system In wp rest api using Basic Auth plugins

Basic authentication system In wp rest api using Basic Auth plugins

plugins/api.php

<?php 
/*Template Name: Api*/

//Using Basic Authentication Plugins

$wp_request_url = 'http://localhost/notebook/wp-json/wp/v2/posts/';

$wp_request_headers = array('Authorization' => 'Basic ' . base64_encode( 'mynotebook:mynotebook' ));
print_r($wp_request_headers);
$body = array('title' => 'Lorem Ipsum ', 'content'=>'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.');

$wp_delete_post_response = wp_remote_request(
  $wp_request_url,
  array(
      'method'    => 'DELETE',
      'headers'   => $wp_request_headers,
      'body' 			=> $body
  )
);
echo wp_remote_retrieve_response_code( $wp_delete_post_response ) . ' ' . 
wp_remote_retrieve_response_message( $wp_delete_post_response );


//end basic auth
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment