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

php中文网
发布: 2016-06-06 20:18:34
原创
1582人浏览过

  1. 描述你的问题

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

  1. 贴上相关代码

    芒果商城系统GSHOP
    芒果商城系统GSHOP

    芒果系统GSHOP 纯静态商城系统,你还在为商城的优化而苦恼?GSHOP是全站纯静态商城系统,一键seo优化功能解决seo问题,自定义URL链接解决商城同质化问题;多页面显示:动态页、伪静态页面、纯静态页面增加收录,提升网站权重,提升流量等。安全稳定、功能强大的商城系统。1、芒果商城系统基于 php5.0开发,企业级应用。2、产品功能Ajax设计,响应速度更快,购物体验更好。3、全新密钥存放机制,

    芒果商城系统GSHOP 0
    查看详情 芒果商城系统GSHOP
<?php
/*
Plugin Name: My Plugin
Plugin URI: http://wordpress.org/plugins/my-plugin/
Description: A RESTful API for WordPress
Version: 1.1.1
Author: Dan Phiffer
Author 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 hooks
add_action('init', 'my_plugin_init');
register_activation_hook( __FILE__, 'my_plugin_activation');
register_deactivation_hook( __FILE__, 'my_plugin_deactivation');

?>
登录后复制

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

回复内容:

  1. 描述你的问题

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

  1. 贴上相关代码

<?php
/*
Plugin Name: My Plugin
Plugin URI: http://wordpress.org/plugins/my-plugin/
Description: A RESTful API for WordPress
Version: 1.1.1
Author: Dan Phiffer
Author 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 hooks
add_action('init', 'my_plugin_init');
register_activation_hook( __FILE__, 'my_plugin_activation');
register_deactivation_hook( __FILE__, 'my_plugin_deactivation');

?>
登录后复制

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

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

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

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

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

"$myPluginBase/(.+)\$" => "index.php?{$myPluginBase}=\$matches[1]"
登录后复制
相关标签:
WPS零基础入门到精通全套教程!
WPS零基础入门到精通全套教程!

全网最新最细最实用WPS零基础入门到精通全套教程!带你真正掌握WPS办公! 内含Excel基础操作、函数设计、数据透视表等

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号