Skip to content

Instantly share code, notes, and snippets.

@vivianspencer
Created December 29, 2015 09:23
Show Gist options
  • Save vivianspencer/f5432b27350e6e444c38 to your computer and use it in GitHub Desktop.
Save vivianspencer/f5432b27350e6e444c38 to your computer and use it in GitHub Desktop.
Wordpress Virtual Page
<?php
function my_rewrite_init()
{
add_rewrite_rule( 'your-desired-path', 'index.php?your-query-var=1', 'top' );
}
add_action( 'init', 'my_rewrite_init' );
function my_rewrite_query_vars( $query_vars )
{
$query_vars[] = 'your-query-var';
return $query_vars;
}
add_action( 'query_vars', 'my_rewrite_query_vars' );
function my_rewrite_redirect()
{
global $wp_query;
if ( array_key_exists( 'your-query-var', $wp_query->query_vars ) ) {
include( 'your-desired-template.php' );
exit();
}
return;
}
add_action( 'template_redirect', 'my_rewrite_redirect' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment