Skip to content

Instantly share code, notes, and snippets.

@everaldomatias
Last active September 7, 2019 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save everaldomatias/d290c00528ebf879ae987e6a28ecff46 to your computer and use it in GitHub Desktop.
Save everaldomatias/d290c00528ebf879ae987e6a28ecff46 to your computer and use it in GitHub Desktop.
<?php
/**
* Add virtual page on WordPress hierarchy
* @link https://metabox.io/how-to-create-a-virtual-page-in-wordpress/
*/
add_filter( 'generate_rewrite_rules', function ( $wp_rewrite ){
$wp_rewrite->rules = array_merge(
['example/(\w+)/?$' => 'index.php?exp=$matches[1]'],
$wp_rewrite->rules
);
} );
add_filter( 'query_vars', function( $query_vars ){
$query_vars[] = 'exp';
return $query_vars;
} );
add_action( 'template_redirect', function(){
$exp = get_query_var( 'exp' );
if ( $exp ) {
include plugin_dir_path( __FILE__ ) . 'templates/example.php';
die;
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment