Have a look at the following ..
function my_parse_request
allows for the template to override.
function my_query_vars
adds your custom argument to the WordPress white listed args
function my_init
registers the rewrite.
function find_custom_template
finds your template in either a parent or child theme.
remember you will need to flush() your permalinks for the rewrite. Just do this either from the Admin>settings>permalink>save or flush through code on plugin activation (not on the init hook !)
// redirect to the signup template file
add_action( 'parse_request', 'my_parse_request' );
function my_parse_request( &$wp ) {
if ( array_key_exists( 'custom-travel-advice', $wp->query_vars ) ) {
/* Allow themes to override the signup template with the file 'page-signup.php'
* in either the parent or child theme.
*/
if ( $template_found = find_custom_template( 'page-signup.php' ) ) {
include( $template_found );
exit();
}
}
}
add_action( 'query_vars', 'my_query_vars' );
/**
* whitelist plugin defined query variable within WordPress
*
* @access public
* @param $query_vars
* @return $query_vars
*/
function my_query_vars( $query_vars ) {
$query_vars[] = 'custom-travel-advice';
return $query_vars;
}
add_action( 'init', 'my_init' );
/**
* Registers all required code
*
* @access public
* @return void
*/
function my_init() {
// add rewrite on the frontend and amdmin side for any furture flush of the rewrites
// the query variable 'custom-travel-advice' is used
add_rewrite_rule( 'travel-advice/?$', 'index.php?custom-travel-advice=true', 'top' );
}
/**
* Finds a custom template is available, if present allows the child or
* parent theme to override the plugin templates.
*
* @return False if not found otherwise the template file location given within the site
*/
function find_custom_template( $template_wanted = '' ) {
$plugin_template_file = false;
if ( locate_template( $template_wanted ) != '' ) {
// locate_template() returns path to file
// if either the child theme or the parent theme have overridden the template
$plugin_template_file = locate_template( $template_wanted );
}
return $plugin_template_file;
}