Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
WP_Rewrite endpoints demo
Plugin Name: WP_Rewrite endpoints demo
Description: A plugin giving example usage of the WP_Rewrite endpoint API
Plugin URI:
Author: Jon Cave
Author URI:
function makeplugins_endpoints_add_endpoint() {
// register a "json" endpoint to be applied to posts and pages
add_rewrite_endpoint( 'json', EP_PERMALINK | EP_PAGES );
add_action( 'init', 'makeplugins_endpoints_add_endpoint' );
function makeplugins_endpoints_template_redirect() {
global $wp_query;
// if this is not a request for json or it's not a singular object then bail
if ( ! isset( $wp_query->query_vars['json'] ) || ! is_singular() )
// output some JSON (normally you might include a template file here)
add_action( 'template_redirect', 'makeplugins_endpoints_template_redirect' );
function makeplugins_endpoints_do_json() {
header( 'Content-Type: application/json' );
$post = get_queried_object();
echo json_encode( $post );
function makeplugins_endpoints_activate() {
// ensure our endpoint is added before flushing rewrite rules
// flush rewrite rules - only do this on activation as anything more frequent is bad!
register_activation_hook( __FILE__, 'makeplugins_endpoints_activate' );
function makeplugins_endpoints_deactivate() {
// flush rules on deactivate as well so they're not left hanging around uselessly
register_deactivation_hook( __FILE__, 'makeplugins_endpoints_deactivate' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment