Create a simple GET endpoint

add_action( 'rest_api_init', function () {
  register_rest_route( 'my_endpoint', '/do_something', array(
    'methods' => 'GET',
    'callback' => '\MyNamespace\my_endpoint_callback',
    'permission_callback' => '__return_true'
  ) );
} );

function my_endpoint_callback ($Request = null)
{
  $Name = "";
  if (isset($_REQUEST['name']))
    $Name= $_REQUEST['name'];

  $Output = array();
  $Output['Result'] = "Hi there: $Name";

  echo json_encode($Output);
}
//You can use these URLs to access it:
//https://my_domain.com/?rest_route=/my_endpoint/do_something&name=John
//https://my_domain.com/wp-json/my_endpoint/do_something&name=John

Create a simple POST endpoint

add_action( 'rest_api_init', function () {
  register_rest_route( 'my_endpoint', '/do_something', array(
    'methods' => 'POST',
    'callback' => '\MyNamespace\my_endpoint_callback',
    'permission_callback' => '__return_true'
  ) );
} );

function my_endpoint_callback ($Request = null)
{
  $Response= array();
  $Parameters = $Request->get_params();
  $payment_method = sanitize_text_field($Parameters['payment_method']);
  $Name = sanitize_text_field($Parameters['name']);

  $Output = array();
  $Output['Result'] = "Hi there: $Name";

  echo json_encode($Output);
}
//You can use these URLs to access it:
//https://my_domain.com/?rest_route=/my_endpoint/do_something
//https://my_domain.com/wp-json/my_endpoint/do_something

Authenticating the current user in a call to the endpoint

A REST api request is by default non-authenticated. If you want to use things like get_current_user_id() in your endpoint handling code, you need to pass a nonce to the endpoint when calling it.

If using POST then ensure you include a POST parameter named ‘_wpnonce’.

The value to put in it can be obtained using:

  $WpNonce = wp_create_nonce('wp_rest');      //Create a nonce for our REST api request

Debugging a POST endpoint

//To debug this endpoint: 
/*
  $WpNonce = wp_create_nonce('wp_rest');      //Create a nonce for our REST api request
  $HtmlOutput = <<<_END
    <form action="/?rest_route=/my_endpoint/do_something" method="POST">
      <input type="hidden" name="_wpnonce" value="$WpNonce" />
      
      <label>A value you need to pass:</label>
      <input type="text" name="MyValueName" style="width:200px;" value="">
      
      <input type="submit" value="Test Endpoint" />
    </form>
_END;
  echo $HtmlOutput
*/
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *