Skip to content

Instantly share code, notes, and snippets.

@twentyfortysix
Last active February 13, 2016 19:43
Show Gist options
  • Save twentyfortysix/eedf37527e2c15347da1 to your computer and use it in GitHub Desktop.
Save twentyfortysix/eedf37527e2c15347da1 to your computer and use it in GitHub Desktop.
WP - create custom json feed
// register json feed
class custom_feed {
public $feed = 'json';
public function __construct() {
add_action( 'init', array( $this, 'init' ) );
}
public function init() {
// feed name to access in URL eg. /feed/custom-xml/
add_feed( $this->feed, array( $this, 'json' ) );
}
public function json() {
global $wp_query;
$n = (!empty($wp_query->query_vars['number'])) ? $wp_query->query_vars['_post'] : 1;
$range = (!empty($wp_query->query_vars['range'])) ? $wp_query->query_vars['range'] : 'all';
$data = get_data($n, $range, 'json');
header('Content-Type: application/json');
echo $data;
}
}
$custom_feed = new custom_feed();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment