Forked from shanaver/json-rest-api-custom-post-type.php
Last active
August 29, 2015 14:11
-
-
Save bitflower/6d8a41a736135771be18 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Notes: | |
* | |
* activate the json-rest-api plugin - then add this code to your functions.php file | |
* | |
* now the following ajax call should work: | |
* | |
jQuery.ajax({ | |
url : 'http://localhost/wp-json.php/projects', | |
type: 'GET', | |
}); | |
* | |
*/ | |
if (class_exists('WP_JSON_CustomPostType')) | |
{ | |
class WP_JSON_Projects extends WP_JSON_CustomPostType | |
{ | |
/** | |
* Base route name | |
* | |
* @var string Route base (e.g. /my-plugin/my-type) | |
*/ | |
protected $base = '/projects'; | |
/** | |
* Associated post type | |
* | |
* @var string Type slug | |
*/ | |
protected $type = 'projects'; | |
public function registerRoutes( $routes ) { | |
$post_routes = array( | |
'/projects' => array( | |
array( array( $this, 'getPosts' ), WP_JSON_Server::READABLE ), | |
) | |
); | |
return array_merge( $routes, $post_routes ); | |
} | |
}/* end class */ | |
function json_api_project_filters($server) { | |
global $wp_json_posts, $wp_json_pages, $wp_json_media, $wp_json_taxonomies; | |
$wp_json_projects = new WP_JSON_Projects($server); | |
add_filter( 'json_endpoints', array( $wp_json_projects, 'registerRoutes' ), 0 ); | |
} | |
add_action( 'wp_json_server_before_serve', 'json_api_project_filters', 10, 1 ); | |
}/* end if */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment