server = $server; } /** * Register the user-related routes * * @param array $routes Existing routes * @return array Modified routes */ public function register_routes( $routes ) { $user_routes = array( // /users/me is an alias, and simply redirects to /users/ '/posts/slug' => array( array( array( $this, 'get_post' ), WP_JSON_Server::READABLE ), ), ); return array_merge( $routes, $user_routes ); } function get_ID_by_slug($page_slug) { return url_to_postid($page_slug); } /* reference: https://github.com/mcnasby/wp-json-api-menu-controller/blob/master/api-controllers/menus.php */ public function get_post( $slug, $context = 'view' ) { //return "Test".$slug. " ".$this->get_ID_by_slug($slug) ." ". get_category_by_path($slug,false)->cat_ID; $postId = $this->get_ID_by_slug($slug); if(!empty($postId)){ $wp_server_posts = new WP_JSON_Posts( $this->server ); return $wp_server_posts->get_post($postId, $context); } $category = get_category_by_path($slug,false); if($category){ $menu = array( "id"=>"-1", "parent_id"=>"-1", "menu_order"=>"-1", "name"=>$category->name, "object_type"=>"category", "object_id"=>$category->cat_ID, "link"=> json_url("/taxonomies/category/terms/".$category->cat_ID) ); return $menu; } list($slug, $remaining) = split('/', $slug, 2); $tag = get_term_by('slug', $remaining, 'post_tag'); if($tag){ $menu = array( "id"=>"-1", "parent_id"=>"-1", "menu_order"=>"-1", "name"=>$tag->name, "object_type"=>"tag", "object_id"=>$tag->term_id, "link"=> json_url("/taxonomies/post_tag/terms/".$tag->term_id) ); return $menu; } return "no page found"; } } ?>