Last active
February 13, 2016 19:43
-
-
Save twentyfortysix/eedf37527e2c15347da1 to your computer and use it in GitHub Desktop.
WP - create custom json feed
This file contains hidden or 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
// 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