I'm working on a plugin that I need to load some custom code. However, I want it to load within the template, while still giving me the ability to change the page title, meta description, etc.
I've got a rewrite rule setup that looks like this:
function cc_rewrite_rules()
{
add_rewrite_rule('^page/form/([^/]*)-([^/]*)?$',
'/wp-content/plugins/cc-plugin/pages/form.php?sign1=$matches[1]&sign2=$matches[2]',
'top'
);
}
Anything I add to form.php loads, but it seems to load with whatever else exists at the base URL. How can I construct this page so that it loads as a stand-alone file but using the active WordPress theme?
Also, how can I set the page title and meta description from within form.php?
I'm working on a plugin that I need to load some custom code. However, I want it to load within the template, while still giving me the ability to change the page title, meta description, etc.
I've got a rewrite rule setup that looks like this:
function cc_rewrite_rules()
{
add_rewrite_rule('^page/form/([^/]*)-([^/]*)?$',
'/wp-content/plugins/cc-plugin/pages/form.php?sign1=$matches[1]&sign2=$matches[2]',
'top'
);
}
Anything I add to form.php loads, but it seems to load with whatever else exists at the base URL. How can I construct this page so that it loads as a stand-alone file but using the active WordPress theme?
Also, how can I set the page title and meta description from within form.php?
Share Improve this question edited Jul 28, 2019 at 18:08 fuxia♦ 107k39 gold badges255 silver badges459 bronze badges asked Jul 28, 2019 at 17:14 DexterDexter 1076 bronze badges 01 Answer
Reset to default 1add_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()
.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745277791a4620135.html
评论列表(0条)