Skip to content

Instantly share code, notes, and snippets.

@clonesia
Created February 28, 2020 10:17
Show Gist options
  • Save clonesia/53127d41014095c58feb6a7451832c17 to your computer and use it in GitHub Desktop.
Save clonesia/53127d41014095c58feb6a7451832c17 to your computer and use it in GitHub Desktop.
<?php # -*- coding: utf-8 -*-
/**
* Plugin Name: T5 Endpoint Example
* Description: Adds a permalink endpoint to posts named <code>epex</code>
*/
add_action( 'init', 't5_add_epex' );
function t5_add_epex()
{
add_rewrite_endpoint( 'epex', EP_PERMALINK );
}
add_action( 'template_redirect', 't5_render_epex' );
/**
* Handle calls to the endpoint.
*/
function t5_render_epex()
{
if ( ! is_singular() or ! get_query_var( 'epex' ) )
{
return;
}
// You will probably do something more productive.
$post = get_queried_object();
print '<pre>' . htmlspecialchars( print_r( $post, TRUE ) ) . '</pre>';
exit;
}
add_filter( 'request', 't5_set_epex_var' );
/**
* Make sure that 'get_query_var( 'epex' )' will not return just an empty string if it is set.
*
* @param array $vars
* @return array
*/
function t5_set_epex_var( $vars )
{
isset( $vars['epex'] ) and $vars['epex'] = true;
return $vars;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment