Skip to content

Instantly share code, notes, and snippets.

@dabernathy89
Last active February 3, 2017 17:08
Show Gist options
  • Save dabernathy89/e969e07f03c44f2515540b9fdcde5adc to your computer and use it in GitHub Desktop.
Save dabernathy89/e969e07f03c44f2515540b9fdcde5adc to your computer and use it in GitHub Desktop.
class SimpleEndpoint {
public function hooks() {
add_action( 'init', array( $this, 'add_endpoint' ) );
add_action( 'template_redirect', array( $this, 'handle_request' ) );
}
public function add_endpoint() {
add_rewrite_tag( '%foobar%', '([^&]+)' );
add_rewrite_rule( '^(foobar)/?$', 'index.php?$matches[1]=1', 'top' );
}
public function handle_request() {
if ( get_query_var('foobar') === '1' ) {
wp_send_json( 'foo' => 'bar' );
}
}
}
$simple_endpoint = new SimpleEndpoint;
$simple_endpoint->hooks();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment