我已经包含了一些代码,每当我需要添加Wordpress重写规则时,我都会使用这些代码。它将其他规则注入到正常的Wordpress重写逻辑中,以便您可以根据URL将Wordpress定向到特定的模板文件。您可以根据需要对其进行修改,方法是向create\\u rewrite\\u rules()函数添加更多规则,并向add\\u query\\u vars()函数添加其他query\\u vars。
<?php // Forums Class //
$ForumsCode = new Forums();
register_activation_hook( __file__, array($ForumsCode, \'activate\') );
add_filter(\'rewrite_rules_array\', array($ForumsCode, \'create_rewrite_rules\'));
add_filter(\'query_vars\',array($ForumsCode, \'add_query_vars\'));
add_filter(\'admin_init\', array($ForumsCode, \'flush_rewrite_rules\'));
add_filter(\'template_include\', array($ForumsCode, \'template_redirect_intercept\'));
class Forums {
function activate() {
global $wp_rewrite;
$this->flush_rewrite_rules();
}
function create_rewrite_rules($rules) {
global $wp_rewrite;
$newRule = array(\'forums/(.+)\' => \'index.php?forumdata=\'.$wp_rewrite->preg_index(1));
$newRule2 = array(\'forums/questions/(.+)\' => \'index.php?questions=true&forumdata=\'.$wp_rewrite->preg_index(1));
$newRules = $newRule + $newRule2 + $rules;
return $newRules;
}
function add_query_vars($qvars) {
$qvars[] = \'forumdata\';
return $qvars;
}
function flush_rewrite_rules() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
function template_redirect_intercept($template) {
global $wp_query;
if ($wp_query->get(\'forumdata\')) {
$template = get_bloginfo(\'template_url\') . \'/page-forums.php\';
}
return $template;
}
function pushoutput($message) {
$this->output($message);
}
function output( $output ) {
header( \'Cache-Control: no-cache, must-revalidate\' );
header( \'Expires: Mon, 26 Jul 1997 05:00:00 GMT\' );
echo json_encode( $output );
}
}