请看以下内容。。
函数my\\u parse\\u request允许模板重写。
函数my\\u query\\u vars将自定义参数添加到WordPress白名单参数
函数my\\u init注册重写。
函数find\\u custom\\u template在父主题或子主题中查找模板。
remember 您需要刷新()永久链接以进行重写。只需从Admin>settings>permalink>save或flush-through-code-on-plugin-activation(而不是在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;
}