WordPress添加重写规则以指向插件文件 [英] Wordpress add rewrite rule to point to plugin file

查看:84
本文介绍了WordPress添加重写规则以指向插件文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我尝试了其他其他stackoverflow用户的不同建议,但是我没有任何运气.

Firstly, I have tried several different suggestions from other stackoverflow users but I haven't had any luck.

我正在尝试从插件内部构建一种api.该任务是让外部系统在我的插件中调用一个URL,以便它启动内部过程.

I'm trying to build a kind of api from inside a plugin. The task is to let an external system call a URL within my plugin in order for it to initiate an internal procedure.

当前,我有一个带有构造函数的类.这是在该构造函数内部.

Currently I have a class which has a contructor. This is inside that constructor.

add_action( 'init', 'my_rewrite' );
function my_rewrite() {
  global $wp_rewrite;

  $plugin_url = plugins_url( 'my-api.php', __FILE__ );
  $plugin_url = substr( $plugin_url, strlen( home_url() ) + 1 );

  add_rewrite_rule('/my-api/(.*)', $plugin_url ,'top');

  $wp_rewrite->flush_rules(true);
}

然后在我的htaccess文件中生成一个RewriteRule.

This then generates a RewriteRule in my htaccess file.

RewriteRule ^/my-api/(.*) /wp-content/plugins/my-api/classes/my-api.php [QSA,L]

下面是用于上下文的整个.htaccess文件

Below is the whole .htaccess file for context

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^/slurp-api/(.*) /wp-content/plugins/slurp/classes/my-api.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

无论出于何种原因,当我访问site.dev/my-api时,我看到的都是404页面,而不是应从my-api.php运行的echo语句

For whatever reason when I visit site.dev/my-api I see a 404 page rather than the echo statement that should run from my-api.php

开发站点正在通过mamp pro运行,如果有帮助的话.

The dev site is being run through mamp pro if that is any help.

任何有关此重写为何不公平的指针将不胜感激.

Any pointers as to why this rewrite isn't playing fair would be greatly appreciated.

谢谢

推荐答案

因此,经过反复修改后,我得出了正确的答案. 我将此行更改为

So, after much tinkering I came upon the correct answer. I changed this line to

add_rewrite_rule('my-api', $plugin_url ,'top');

我对此进行了许多尝试,但最终成功了.感谢您的帮助.

I tried many variations on this but this one finally worked. Thanks for the help.

这篇关于WordPress添加重写规则以指向插件文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆