也许你可以用query_vars
过滤器和parse_request
行动
add_action(\'init\', \'initInternalWPRewrite\', 10);
// Add your rewrite rule, you don\'t need to create a wordpress page
function initInternalWPRewrite () {
add_rewrite_rule(\'^diagnostics/?$\', \'index.php?diagnostics=1\', \'top\');
}
add_action(\'init\', \'rewriteRulesCacheInit\', 10);
// This method is useful to avoid manual flush rewrite cache
function rewriteRulesCacheInit () {
if (!get_option(\'rewrite_rules_cache\')) {
add_option(\'rewrite_rules_cache\', true);
}
}
// With this method you don\'t need to flush cache
add_action(\'init\', \'rewriteRulesFlushCache\', 20);
function rewriteRulesFlushCache () {
if (get_option(\'rewrite_rules_cache\')) {
flush_rewrite_rules();
delete_option(\'rewrite_rules_cache\');
}
}
// Add your custom query var
add_filter(\'query_vars\', \'queryVars\');
function queryVars ($query_vars) {
$query_vars[] = \'diagnostics\';
return $query_vars;
}
// Render your PHP content, please, avoid include PHP or if you want to include PHP file, ensure is defined ABSPATH constant to avoid direct execution
// REMEMBER: exit or die, if you don\'t do this, WordPress will be loaded.
add_action(\'parse_request\', \'renderDiagnosticPage\');
function renderDiagnosticPage ($wp_query) {
if (array_key_exists(\'diagnostics\', $wp_query->query_vars)) {
//your code
exit();
}
return;
}
更多信息:
查询变量筛选器:https://codex.wordpress.org/Plugin_API/Filter_Reference/query_vars
分析请求操作:https://codex.wordpress.org/Plugin_API/Action_Reference/parse_request
添加\\u重写\\u规则方法:https://codex.wordpress.org/Rewrite_API/add_rewrite_rule希望这对您有所帮助。