markzero/wp-on-routes

WordPress插件,通过允许您添加自定义路由来增加API-like功能。

安装: 6

依赖关系: 0

建议者: 0

安全: 0

星标: 13

关注者: 4

分支: 1

开放问题: 3

类型:wordpress-plugin

dev-master 2015-10-13 09:47 UTC

This package is not auto-updated.

Last update: 2024-10-02 09:49:48 UTC


README

WordPress on Routes是一个受Ruby微框架启发的WordPress插件。它添加了向您的WordPress实例添加自定义路由的功能。适用于表单提交、API-like功能等。

它还托管在插件仓库中:https://wordpresstheme.cn/plugins/wp-on-routes/

安装

这里也适用传统的插件安装方法 - 下载或克隆到plugins/目录,然后激活。

不涉及UI。

基本用法

在您的functions.php文件中

$routing = \WoR\Main::get_instance();

$routing->add_routes(
  array(
    'get' => array(
      'path' => '/foo/bar',
      'body' => 'Hello Buz!',
      'headers' => array(
        'Content-Type' => 'text/html; charset=UTF-8',
        'exclude' => array(
          'x-powered-by', 'x-pingback'
        )
      )
    )
  )
);

当然,您必须检查类是否存在(例如在lib/routes.php文件中)

if (!class_exists('\WoR\Main')) {
  return;
}

并在functions.php中使用它

require_once('lib/routes.php');

请记住,由于使用命名空间,您的PHP安装版本必须为>= 5.3.0

扩展用法

function wor_dump() {
  var_dump($_GET);
}

add_action('wor_action', 'wor_dump');

$routing = \WoR\Main::get_instance();

$routing->add_routes(
  array(
    'get' => array(
      'path' => '/foo/*/bar/:p1?',
      'action' => 'wor_action',
      'agent' => '/Firefox/',
      'include_template' => true
    )
  )
);

在上面的示例中,如果目标是/foo/a/b/c/bar/test,浏览器将以HTTP状态200回答,以下代码,包括头部和尾部,仅在Firefox浏览器中

array (size=2)
  'p1' => string 'test' (length=4)
  'splats' => 
    array (size=1)
      0 => string 'a/b/c' (length=5)

详细信息

此时有几个功能

  1. 向您的WordPress安装添加自定义路由
  2. 设置方法GET/POST/DELETE等。
  3. 设置正文(作为文本或模板)或操作(使用add/do_action)。如果两者都已定义,则action优先于body
  4. 设置头部(例如,'Content-Type' => 'text/html; charset=UTF-8')
  5. 排除头部(例如,'Set-Cookie')
  6. 设置参数如/my/route/:param1/:param2或作为splits /foo/*/bar
  7. 添加代理或通过代理过滤,使用正则表达式
  8. 代理过滤器用于否定逻辑(例如,/^((?!Firefox).)*$/,它告诉“除了Firefox之外的任何浏览器”)
  9. 包含头部和尾部

您可以设置的选项

  1. path(字符串)
  2. body OR action(字符串)
  3. agent(字符串/正则表达式)
  4. include_template(布尔值;默认:false)
  5. headers(数组)
  • exclude(数组)

测试

参考tests/instructions.txt以阅读如何测试您的WordPress网站的输出。

我使用wp-cli生成测试环境: https://github.com/wp-cli/wp-cli/wiki/Plugin-Unit-Tests

因此,在测试之前运行类似的命令:bash bin/install-wp-tests.sh wor_test_db root root localhost latest