添加重写规则的插件:用的.htaccess的插件文件夹或使用Word preSS功能插件、重写、文件夹、规则

2023-09-02 09:53:25 作者:谁纵我一世猖狂

我需要在我的插件添加重写规则,并与我的code分发。所有工作正常,如果我把这个规则在Word中preSS根文件夹中的.htaccess,但我需要用我的规则分配插件。

我尝试把.htaccess中的插件文件夹中,并尝试使用的 add_rewrite_rule 功能,但并没有任何工作。

下面的的.htaccess code的正常工作在Word preSS根文件夹,但没有在我的插件文件夹的作品:

 < IfModule mod_rewrite.c>

RewriteEngine叙述上
重写规则我-插件/页/ TP(。*)。PHP $的wp-content /插件/我-插件/页/ request.php?PID = $ 1

< / IfModule>
 

我尝试后续code。在我的插件,但并没有工作之一:

 的add_filter(query_vars','add_query_vars');
功能add_query_vars($ query_vars)
{
    $ query_vars [] ='PID';
    返回$ query_vars;
}
add_action('init'的,'add_init');
功能add_init()
{
    $ plugin_url ='的路径对我-插件文件夹;
    add_rewrite_rule(我的-插件/页/ TP(。*)。PHP'
                  ,$ plugin_url。 '?页/ request.php PID = $匹配[1]','顶');

    全球$ wp_rewrite;
    $ wp_rewrite-> flush_rewrite_rules(); //我知道这应该叫只有一次,但我把它放在这里只是为了让简单的样品code
}
 
给文件和文件夹添加备注 使用windows快捷方式进行文件管理

不过,我总是得到未找到该URL的错误。 我做错了什么?我该怎么办我需要什么?我搜索了类似的问题,但没有解决我的问题。

我的插件文件夹结构为:

主目录:/可湿性粉剂内容/插件/我-插件 ------ /页(子文件夹) ------------- / request.php(脚本应该接收请求)

解决方案   

注意:Word preSS改写API是不一样的Apache的重写模块。   WP重写API不会重定向请求到另一个URL,它用来   解析当前URL,并填写 query_vars 阵列。

这个问题是在你的 add_rewrite_rule 函数调用的第二个参数。它从的index.php 启动,然后应该有你的论点,如 PID ,例如:

 的index.php?PID = $匹配[1] ......
 

所以,你的 add_init 函数应该是这样的:

  add_action('init'的,'wpse8170_add_init');
功能wpse8170_add_init()
{
    add_rewrite_rule('。我-插件/页/ TP(*) PHP','index.php的PID = $匹配[1]?','顶');
}
 

不要忘记通过访问设置冲洗重写规则资讯» 永久链接页。

延伸阅读:

的重写API:基础知识 的重写API:文章类型:放大器;分类

I need add a rewrite rule in my plugin, and distribute it with my code. All works fine if I put the rule in the .htaccess in the WordPress root folder, but I need distribute the plugin with my rule.

I try to put a .htaccess inside the plugin folder and try to use the add_rewrite_rule function but doesn't works either.

Here the .htaccess code that works correctly in WordPress root folder but doesn't works in my plugin folder:

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteRule my-plugin/pages/tp(.*).php$ wp-content/plugins/my-plugin/pages/request.php?pid=$1

</IfModule>

I try the follow code in my plugin but doesn't works either:

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
}

But I always get the error that the URL wasn't found. What I'm doing wrong? How can I do what I need? I searched for similar questions but none solve my problem.

My plugin folder structure is:

Main folder: /wp-content/plugins/my-plugin ------ /pages (sub folder) -------------/request.php (script that should receive the request)

解决方案

NOTE: WordPress Rewrite API is not the same as Apache Rewrite module. WP Rewrite API doesn't redirect a request to another URL, it used to parse current URL and fill query_vars array.

The issue is in the second parameter of you add_rewrite_rule function call. It has to start from index.php? and then there should be your arguments, like pid, for example:

"index.php?pid=$matches[1]...."

So your add_init function should be like this:

add_action( 'init', 'wpse8170_add_init' );
function wpse8170_add_init()
{
    add_rewrite_rule('my-plugin/pages/tp(.*).php', 'index.php?pid=$matches[1]', 'top');
}

Don't forget to flush rewrite rules by visiting Settings » Permalinks page.

Further reading:

The Rewrite API: The Basics The Rewrite API: Post Types & Taxonomies