我安装了Monkey Man Rewrite Analyzer,我安装了一个规则,它显示在列表中,如果我用Monkey Man测试它,它说它会启动,但实际上没有,怎么了?
add_action(\'init\', array($this, \'register_crr_jsonwebservice\'), 99);
public function register_crr_jsonwebservice() {
//Add the rewrite rule for this specific json controller
add_rewrite_tag( \'%ticket%\', \'([a-zA-Z0-9]+)\');
add_rewrite_rule( \'cjson/fullticket/([a-zA-Z0-9]+)/\', \'index.php?p=1312&ticket=$matches[1]\', \'top\' );
flush_rewrite_rules();
}
在MMRA中测试此规则:
cjson/全票/231412tgsxcasfasdfas/
工作正常,直接在wordpress上复制相同的url进行测试?404,未找到。。。我做错了什么?
SO网友:david.binda
老实说,我无法调试您的代码,但这一个很有效:
function register_crr_jsonwebservice( $wp_rewrite ) {
//Add the rewrite rule for this specific json controller
add_rewrite_tag( \'%ticket%\', \'([a-zA-Z0-9]+)\');
$wp_rewrite->rules = array(
\'cjson/fullticket/?([a-zA-Z0-9]{1,})/?$\' => $wp_rewrite->index . "?page_id=243&ticket=".$matches[1]
) + $wp_rewrite->rules;
}
add_action( \'generate_rewrite_rules\', \'register_crr_jsonwebservice\' );
不要调用flush\\u rewrite\\u rules();来自此函数-将导致错误。也许add\\U rewrite\\U标记必须从init上的单独函数中进行缩放-尚未测试。