Skip to content

Instantly share code, notes, and snippets.

@torounit
Created April 12, 2013 00:56
Show Gist options
  • Save torounit/5368443 to your computer and use it in GitHub Desktop.
Save torounit/5368443 to your computer and use it in GitHub Desktop.
WordPressの投稿をJSONで吐き出す。
Class WP_JSON_API {
public function __construct() {
add_action( "init", array( &$this, "add_query_var" ),10);
add_action( "init", array( &$this, "json_actions" ),11);
}
public function add_query_var() {
global $wp;
$wp->add_query_var( "json" );
}
public function json_actions() {
if( $_GET["json"] == "true") {
add_action( "pre_get_posts", array(&$this, "pre_get_posts"));
add_action( "wp", array(&$this, "json_format"));
}
}
public function pre_get_posts( $query ) {
$query->set( "nopaging" ,1 );
}
public function json_format( $wp ) {
global $posts;
header( 'Content-Type: application/json' );
echo json_encode( $posts );
exit;
}
}
$jsonapi = new WP_JSON_API;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment