You may want a url to be used for say remote devices to connect to your site and pass information using say URL parameters and a response string.
REST API method
//*************************************
//*************************************
//********** MY API ENDPOINT **********
//*************************************
//*************************************
add_action( 'rest_api_init', function () {
register_rest_route( 'my-api-endpoint-subname', '/my-api-endpoint-name', array(
'methods' => 'POST',
'callback' => '\MyNamespace\odac_my_api_endpoint_callback',
'permission_callback' => '__return_true'
) );
} );
//You can use these URLs to access it (either, both do same thing):
//Endpoint URL: https://thisdomain.com/?rest_route=/my-api-endpoint-subname/my-api-endpoint-name
//Endpoint URL: https://thisdomain.com/wp-json/my-api-endpoint-subname/my-api-endpoint-name
//To debug this endpoint:
/*
$WpNonce = wp_create_nonce('wp_rest'); //Create a nonce for our REST api request
$HomeUrl = home_url();
$HtmlOutput = <<<_END
<form action="$HomeUrl/?rest_route=/my-api-endpoint-subname/my-api-endpoint-name" method="POST">
<input type="hidden" name="_wpnonce" value="$WpNonce" />
<label>My Field 1:</label>
<input type="number" name="MyField1" style="width:100px;" value="">
<input type="submit" value="Test Endpoint" />
</form>
_END;
echo $HtmlOutput;
*/
function odac_my_api_endpoint_callback ($Request = null)
{
try
{
//----- GET POST PARAMETERS -----
//$Response= array();
$Parameters = $Request->get_params();
$MyField1 = (isset($_POST['MyField1'])) ? trim($_POST['MyField1']) : "";
$MyReturnValue = "abcd";
echo json_encode(array('MyReturnValue' => $MyReturnValue));
}
catch (\Exception $e)
{
echo json_encode(array('ErrorMessage' => 'Error: ' . $e->getMessage()));
}
catch (\Error $e)
{
echo json_encode(array('ErrorMessage' => 'Error: ' . $e->getMessage()));
}
}
Page method
You can create a special url and trap it using the code below. However for it to work in all instances you need to have a real wordpress page created with the url. Otherwise when using wget under linux for example, you can get a 404 not found error returned
//*************************************************
//*************************************************
//********** TEMPLATE PAGE ABOUT TO LOAD **********
//*************************************************
//*************************************************
//Hook executes just before WordPress determines which template page to load. It is a good hook to use if you need to do a redirect with full knowledge of the
//content that has been queried.
add_action("template_redirect", 'mysite_template_redirect');
function mysite_template_redirect()
{
//----------------------------
//----- OUR API URL PAGE -----
//----------------------------
//URL to access this api function: "mysitename.com/api-v1"
if (strpos($_SERVER["REQUEST_URI"], '/api-v1') !== false) //<<<There must be a real worldpress page for this url otherwise using wget from linux will not work and give a 404 not found error
{
//GET URL ARGUMENTS
$AuthCode = "";
if (isset($_REQUEST['auth']))
$AuthCode = $_REQUEST['auth'];
die("OK-Auth-" . $AuthCode); //<<<<This string is returned as the output to the device taht has used this url
}
}
