How To Add Custom Endpoints to the WordPress REST API

Sometimes when developing mobile apps in Xamarin Forms, I use WordPress as the backend. And in WordPress I will usually make my own custom post types. This is an example on how I create my own custom REST API endpoints in the WordPress REST API.

Using these free plugins Advanced Custom Fields and Code Snippets and following the instructions on the WordPress Rest API website, this is how I add my own custom endpoints to the WordPress REST API.

  1. I first created a custom post type called  “my-custom-post-type”
  2. Then, I create a new PHP CODE SNIPPET and add the following code:
add_action( 'rest_api_init', function () {
register_rest_route( 'my-custom-enpoint/v1', '/gjhdigital', array(
'methods' => 'GET',
'callback' => 'DoStuff',
'permission_callback' => '__return_true',
) );
} );

function DoStuff($data){
$params = $data->get_params();
$direction = $params['direction'];
if($direction == 'getall'){
return GetAllMyCustomPostTypePosts( $params );
}
else if($direction == 'getbyid'){
return GetAllMyCustomPostTypePostsID( $params );
}
else{
return 'Internal error';
}
}

function GetAllMyCustomPostTypePosts($params){

//https://mywordpresswebsite.com/wp-json/my-custom-enpoint/v1/gjhdigital?direction=getall&devicetype=iphone

$mobiledevice = $params['devicetype'];
if($mobiledevice == 'iphone' || $mobiledevice == 'android'){

$postsArr = [];
$args = array(
'post_type'=> 'my-custom-post-type',
'posts_per_page' => -1
);
$posts = get_posts($args);

foreach($posts as $post){

$dc = new stdClass();
$dc->ChordID = $post->ID;
$dc->AuthorID = $post->post_author;
$dc->Content = $post->post_content;
$dc->Title = $post->post_title;
$dc->CreateDate = $post->post_date;

//get the meta data from the Custom Fields plugin
$postmeta = get_post_meta($post->ID);
if($postmeta){
$dc->Image = $postmeta['image'][0]; //my own custom fields
$dc->Sound = $postmeta['sound'][0]; //my own custom fields
$dc->Video = $postmeta['video'][0]; //my own custom fields
$dc->Subscription = $postmeta['subscription'][0]; //my own custom fields
}
array_push($postsArr, $dc );
}

return $postsArr;
}
else{
return false;
}

}

function GetAllMyCustomPostTypePostsID($params){

//https://mywordpresswebsite.com/wp-json/my-custom-enpoint/v1/gjhdigital?direction=getbyid&postid=198&devicetype=iphone
$mobiledevice = $params['devicetype'];
$postid = $params['postid'];

if($mobiledevice == 'iphone' || $mobiledevice == 'android'){

$postsArr = [];
$posts = get_post($postid);

$dc = new stdClass();
$dc->ChordID = $chords->ID;
$dc->AuthorID = $chords->post_author;
$dc->Content = $chords->post_content;
$dc->Title = $chords->post_title;
$dc->CreateDate = $chords->post_date;

//get the meta data from the Custom Fields plugin
$postmeta = get_post_meta($posts->ID);
if($postmeta){
$dc->Image = $postmeta['image'][0]; //my own custom fields
$dc->Sound = $postmeta['sound'][0]; //my own custom fields
$dc->Video = $postmeta['video'][0]; //my own custom fields
$dc->Subscription = $postmeta['subscription'][0]; //my own custom fields
}
array_push($postsArr, $dc );

return $postsArr; 
}
else{
return false;
}
}

I can then call my custom endpoint in the REST API like this: Notice the custom namespace and endpoint in bold
https://mywordpresswebsite.com/wp-json/my-custom-enpoint/v1/gjhdigital?direction=getall&devicetype=iphone

 

 

 

16220cookie-checkHow To Add Custom Endpoints to the WordPress REST API