Last active
September 7, 2019 00:29
-
-
Save everaldomatias/d290c00528ebf879ae987e6a28ecff46 to your computer and use it in GitHub Desktop.
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
<?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