• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

wordpress 插件开发 – 自定义URL的问题

php 搞代码 3年前 (2022-01-25) 20次浏览 已收录 0个评论
文章目录[隐藏]
  1. 描述你的问题

我参考json-api这个插件的写法,想要自己实现一些数据接口,但是在自定义路由的时候一直不起作用。(我想自定义一个类似 ‘/api3/controlName’ 这样的url接口)

  1. 贴上相关代码

<code class="php"><?php/*Plugin Name: My PluginPlugin URI: http://wordpress.org/plugins/my-plugin/Description: A RESTful API for WordPressVersion: 1.1.1Author: Dan PhifferAuthor URI: http://phiffer.org/*/$myPluginBase = 'api3';function my_plugin_init() {    global $wp_rewrite;    add_filter('rewrite_rules_array', 'my_plugin_rewrites');    add_action('template_redirect', 'my_plugin_template_rewrite');    $wp_rewrite->flush_rules();}function my_plugin_template_rewrite(){    global $myPluginBase;    if( isset( $_REQUEST[ $myPluginBase ] ) ){        echo $_REQUEST[ $myPluginBase ];        exit;    }}function my_plugin_activation() {    // Add the rewrite rule on activation    global $wp_rewrite;    add_filter('rewrite_rules_array', 'my_plugin_rewrites');    $wp_rewrite->flush_rules();}function my_plugin_deactivation() {    // Remove the rewrite rule on deactivation    global $wp_rewrite;    $wp_rewrite->flush_rules();}function my_plugin_rewrites($wp_rules) {    global $myPluginBase;    if (empty($base)) {        return $wp_rules;    }    $my_plugin_rules = array(        "$myPluginBase\$" => "index.php?{$myPluginBase}=info",        "$myPluginBase/(.+)\$" => "index.php?{$myPluginBase}=\$matches[1]"    );    return array_merge($my_plugin_rules, $wp_rules);}// Add initialization and activation hooksadd_action('init', 'my_plugin_init');register_activation_hook( __FILE__, 'my_plugin_activation');register_deactivation_hook( __FILE__, 'my_plugin_deactivation');?></code>

template_redirect这个action应该是生效了,比如访问 /api3=hello 能正常返回 hello,但是如果尝试访问 /api3/hello 则总是返回首页。

回复内容:

  1. 描述你的问题

我参考json-api这个插件的写法,想要自己实现一些数据接口,但是在自定义路由的时候一直不起作用。(我想自定义一个类似 ‘/api3/c

。本文来源gao!%daima.com搞$代*!码网1

搞代gaodaima码ontrolName’ 这样的url接口)

  1. 贴上相关代码

<code class="php"><?php/*Plugin Name: My PluginPlugin URI: http://wordpress.org/plugins/my-plugin/Description: A RESTful API for WordPressVersion: 1.1.1Author: Dan PhifferAuthor URI: http://phiffer.org/*/$myPluginBase = 'api3';function my_plugin_init() {    global $wp_rewrite;    add_filter('rewrite_rules_array', 'my_plugin_rewrites');    add_action('template_redirect', 'my_plugin_template_rewrite');    $wp_rewrite->flush_rules();}function my_plugin_template_rewrite(){    global $myPluginBase;    if( isset( $_REQUEST[ $myPluginBase ] ) ){        echo $_REQUEST[ $myPluginBase ];        exit;    }}function my_plugin_activation() {    // Add the rewrite rule on activation    global $wp_rewrite;    add_filter('rewrite_rules_array', 'my_plugin_rewrites');    $wp_rewrite->flush_rules();}function my_plugin_deactivation() {    // Remove the rewrite rule on deactivation    global $wp_rewrite;    $wp_rewrite->flush_rules();}function my_plugin_rewrites($wp_rules) {    global $myPluginBase;    if (empty($base)) {        return $wp_rules;    }    $my_plugin_rules = array(        "$myPluginBase\$" => "index.php?{$myPluginBase}=info",        "$myPluginBase/(.+)\$" => "index.php?{$myPluginBase}=\$matches[1]"    );    return array_merge($my_plugin_rules, $wp_rules);}// Add initialization and activation hooksadd_action('init', 'my_plugin_init');register_activation_hook( __FILE__, 'my_plugin_activation');register_deactivation_hook( __FILE__, 'my_plugin_deactivation');?></code>

template_redirect这个action应该是生效了,比如访问 /api3=hello 能正常返回 hello,但是如果尝试访问 /api3/hello 则总是返回首页。

我的天哪,你的写的让我有点看不懂。
不过既然你提到你访问 /api3=hello可以正常返回hello 。首先请问它是一个什么类型的地址?

page?post?cat? 建议你先试试下面的代码

<code>// 注册一个链接add_action( 'init', 'api3_rewrites_init' );function api3_rewrites_init(){    add_rewrite_rule(        'api3/(.+)\$',        'index.php?&api3=$matches[1]',        'top'     );}</code>

你的代码,我有一行不太了解

<code>"$myPluginBase/(.+)\$" => "index.php?{$myPluginBase}=\$matches[1]"</code>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:wordpress 插件开发 – 自定义URL的问题

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址