add_rewrite_rule( $regex, $redirect, $after );
Rewrite rules to scripts other than index.php
The
$redirect
argument works slightly differently when redirecting to a custom PHP script because WordPress delegates these redirects to.htaccess
instead of processing them itself. For this reason, querystring variables should be written like$1
instead of$matches[1]
.
Personally, I would use a rewrite rule pointing to index.php
. In this way, the rule will also work outside of Apache.
add_action( 'init', 'se343855_rewrite_rules' );
add_filter( 'query_vars', 'se343855_query_vars' );
function se343855_rewrite_rules()
{
add_rewrite_rule(
'^page/form/([^/-]*)-([^/]*)?$',
// Or maybe:
// '^page/form/([^/-]*)(?:-([^/]*))?$',
//
'index.php?sign1=$matches[1]&sign2=$matches[2]',
'top'
);
}
function se343855_query_vars( $vars )
{
array_push( $vars, 'sign1', 'sign2' );
return $vars;
}
Load a custom template if the query variable is set (sign1
or sign2
):
add_filter( 'template_include', 'se343855_template_include', 50 );
function se343855_template_include( $template )
{
$sign1 = get_query_var('sign1', false);
$sign2 = get_query_var('sign2', false);
if ( $sign1 !== false || $sign2 !== false ) {
// if this code is located in plugin file
$template = plugin_dir_path(__FILE__) . '/pages/form.php';
}
return $template;
}
Use get_query_var() to get value of sign1
or sign2
.
In form.php
file you can use get_header()
and get_footer()
.