这是一个例子:
// register your rewrite rule
add_action('init', function(){
add_rewrite_rule('the-extension$', 'index.php?my_plugin_action=do_stuff', 'top');
});
// register your query variable
add_filter('query_vars', function($vars){
$vars[] = 'my_plugin_action';
return $vars;
});
// process request
add_action('parse_request', function($wp){
// check if this is your request; do nothing if not
if(!isset($wp->query_vars['my_plugin_action']))
return;
// otherwise do your stuff
printf('Hello Pony. You requested "%s"', $wp->query_vars['my_plugin_action']);
exit;
});
这仅在您转到仪表板 > 设置 > 永久链接并单击保存按钮以刷新重写规则后才有效,因为 WP 显然保留了所有重写规则的某种缓存。
然后访问yourwordpresssite.com/the-extension
应该显示 hello 消息