下面是一个快速而肮脏的插件,它向您展示了如何实现这一点(根据您自己的架构和需求调整此示例):
<?php
/*
Plugin Name: Custom rewrite rule test
*/
add_action(
\'plugins_loaded\',
array(Registration::get_instance(), \'setup\')
);
class Registration {
protected static $instance = NULL;
public function __construct() {}
public static function get_instance() {
NULL === self::$instance and self::$instance = new self;
return self::$instance;
}
public function setup() {
add_action(\'init\', array($this, \'rewrite_rules\'));
add_filter(\'query_vars\', array($this, \'query_vars\'), 10, 1);
add_action(\'parse_request\', array($this, \'parse_request\'), 10, 1);
register_activation_hook(__FILE__, array($this, \'flush_rules\' ));
}
public function rewrite_rules(){
add_rewrite_rule(\'registration/?$\', \'index.php?registration=true\', \'top\');
}
public function flush_rules(){
$this->rewrite_rules();
flush_rewrite_rules();
}
public function query_vars($vars){
$vars[] = \'registration\';
return $vars;
}
public function parse_request($wp){
if ( array_key_exists( \'registration\', $wp->query_vars ) ){
include plugin_dir_path(__FILE__) . \'registration.php\';
exit();
}
}
}
- 使用
add_rewrite_rule()
- 添加自定义查询变量“registration”
- 分析请求,检查是否存在“注册”查询变量(如果存在),包括
registration.php
文件
添加
internal WordPress rewrite规则是外部规则的一个,即WordPress环境将在您的
registration.php
例如,如果您需要修改或检索数据库中的数据,您可以。
你内心的榜样registration.php
文件:
<?php
global $wp, $wpdb;
//get query variable: index.php?registration=true
var_dump($wp->query_vars[\'registration\']); //true
//check if user is logged in
if ( is_user_logged_in() ) {
//use $wpdb to get results from database etc
}
//do your registration business logic here etc