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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料