Skip to content

Instantly share code, notes, and snippets.

@janrenn
Created June 27, 2017 09:40
Show Gist options
  • Save janrenn/deaeb9b477615c6d2a627e6af0f19654 to your computer and use it in GitHub Desktop.
Save janrenn/deaeb9b477615c6d2a627e6af0f19654 to your computer and use it in GitHub Desktop.
<?php
//try http://example/wp-json/sap/v1/angular
function sap_rest_angular( $data ) {
$json = new stdClass();
// bloginfo
$json->bloginfo = new stdClass();
$json->bloginfo->name = get_bloginfo('name');
$json->bloginfo->description = get_bloginfo('description');
//$json->bloginfo->wpurl = get_bloginfo('wpurl');
//$json->bloginfo->url = get_bloginfo('url');
//$json->bloginfo->admin_email = get_bloginfo('admin_email');
$counter = 1;
// pages
$json->pages = new stdClass();
$pages = get_pages(array(
'sort_order' => 'asc',
'sort_column' => 'menu_order',
'hierarchical' => 0,
'exclude' => '',
'include' => '',
'meta_key' => '',
'meta_value' => '',
'authors' => '',
'child_of' => 0,
'parent' => -1,
'exclude_tree' => '',
'number' => '',
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish',
));
foreach ($pages AS $page) {
$page->wp_id = (string) $page->ID;
unset($page->ID);
$page->id = (string) $counter;
unset($page->comment_status);
unset($page->ping_status);
unset($page->post_password);
unset($page->to_ping);
unset($page->pinged);
unset($page->post_parent);
unset($page->guid);
unset($page->comment_count);
$json->pages->{$page->post_name} = $page;
$counter++;
}
// categories
$categories = get_categories(array(
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 0,
'exclude' => '',
'include' => '',
'number' => '',
'pad_counts' => false
));
foreach ($categories AS $category) {
$category->id = (string) $counter;
$json->pages->{$category->slug} = $category;
$counter++;
}
return $json;
}
add_action( 'rest_api_init', function () {
register_rest_route( 'sap/v1', '/angular', array(
'methods' => 'GET',
'callback' => 'sap_rest_angular'
) );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment