我需要在我的插件中添加一个重写规则,并将它与我的代码一起分发。如果我将规则放在 WordPress 根文件夹的 .htaccess 中,一切正常,但我需要使用我的规则分发插件。
我尝试在插件文件夹中放置一个 .htaccess 并尝试使用add_rewrite_rule函数,但也不起作用。
这里的 .htaccess 代码在 WordPress 根文件夹中正常工作,但在我的插件文件夹中不起作用:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule my-plugin/pages/tp(.*)\.php$ wp-content/plugins/my-plugin/pages/request.php?pid=$1
</IfModule>
我在我的插件中尝试以下代码,但也不起作用:
add_filter( 'query_vars', 'add_query_vars' );
function add_query_vars( $query_vars )
{
$query_vars[] = 'pid';
return $query_vars;
}
add_action( 'init', 'add_init' );
function add_init()
{
$plugin_url = 'the-path-to-my-plugin-folder';
add_rewrite_rule('my-plugin/pages/tp(.*)\.php'
, $plugin_url . 'pages/request.php?pid=$matches[1]','top');
global $wp_rewrite;
$wp_rewrite->flush_rewrite_rules(); // I know this should be called only one time, but I put it here just to keep simple the sample code
}
但我总是收到找不到 URL 的错误。我做错了什么?我怎样才能做我需要的?我搜索了类似的问题,但没有一个能解决我的问题。
我的插件文件夹结构是:
主文件夹:/wp-content/plugins/my-plugin ------ /pages(子文件夹)-------------/request.php(应该接收请求的脚本)