dsnnvpobaljihv3490 2015-12-08 12:45
浏览 35
已采纳

Wordpress添加重写规则以指向插件文件

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

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);
}

This then generates a RewriteRule in my htaccess file.

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

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

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

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.

Thanks

  • 写回答

2条回答 默认 最新

  • dongwu1410 2015-12-09 09:04
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化