alleyinteractive/wp-path-dispatch
Composer 安装命令:
composer require alleyinteractive/wp-path-dispatch
包简介
Simply and easily add a URL which fires an action, triggers a callback, and/or loads a template.
README 文档
README
Simply and easily add a URL which fires an action, triggers a callback, and/or loads a template.
Installation
You can install the package via composer:
composer require alleyinteractive/wp-path-dispatch
Usage
At any point before init,
\WP_Path_Dispatch\Path_Dispatch()->add_path( [ 'path' => 'some-path', 'callback' => 'some_function' ] );
This will cause http://domain.com/some-path/ to call some_function().
IMPORTANT! You must flush your rewrites after adding a path.
You can add multiple paths at once with add_paths():
\WP_Path_Dispatch\Path_Dispatch()->add_paths( [ [ 'path' => 'some-path', 'callback' => 'some_function', ], [ 'path' => 'custom-feed.json', 'callback' => 'custom_feed', ], [ 'path' => 'custom-feed.xml', 'callback' => 'custom_feed', ], ] );
The dispatch happens on parse_query, so you can then modify the query via
pre_get_posts or do whatever
you have to do. You can even just load a static file and exit if you simply need to render static content.
When the path is loaded, the action dispatch_path_{$path} is fired. You can
hook onto this instead of or
in addition to passing a callback to add_path(s). The callback is optional.
Lastly, you can set custom rewrites if your paths are more complex. In these cases, the 'path' argument essentially becomes a slug. See add_rewrite_rule() for details about 'rule', 'redirect' (rewrite), and 'position'.
Full breakdown of all the path options
\WP_Path_Dispatch\Path_Dispatch()->add_path( [ 'path' => 'some-path', // required 'callback' => 'some_function', // optional 'action' => '', // fire this action instead of dispatch_path_{$path} 'template' => '', // optional 'rewrite' => [ // optional 'query_vars' => [], // optional 'rule' => '', // required (assuming 'rewrite' is set) 'redirect' => 'index.php?dispatch=$matches[1]', // optional 'position' => 'top' // optional ], ] );
Examples
Simplest possible usages: fires the action 'dispatch_path_my-path' at http://domain.com/my-path/
\WP_Path_Dispatch\Path_Dispatch()->add_path( [ 'path' => 'my-path', ] );
This can even be simplified further as:
\WP_Path_Dispatch\Path_Dispatch()->add_path( 'my-path' );
Call the function 'my_function' at http://domain.com/my-path/
\WP_Path_Dispatch\Path_Dispatch()->add_path( [ 'path' => 'my-path', 'callback' => 'my_function', ] );
Load the template file 'dispatch-custom-page.php' at http://domain.com/my-path/
\WP_Path_Dispatch\Path_Dispatch()->add_path( [ 'path' => 'my-path', 'template' => 'custom-page' ] );
Add a custom rewrite rule. Fires the action 'dispatch_path_my-path' at e.g. http://domain.com/my-path/foo/ and sets the query var 'my_path' to 'foo'. This assumes you already registered that query var.
\WP_Path_Dispatch\Path_Dispatch()->add_path( [ 'path' => 'my-rewrite', 'rewrite' => [ 'rule' => 'my-path/(.*)/?', 'redirect' => 'index.php?dispatch=my-rewrite&my_path=$matches[1]' ], ] );
Same as above, but registers the query var automatically, and loads the template 'dispatch-my-page.php'.
\WP_Path_Dispatch\Path_Dispatch()->add_path( [ 'path' => 'my-rewrite', 'rewrite' => [ 'rule' => 'my-path/(.+)/?', 'redirect' => 'index.php?dispatch=my-rewrite&my_path=$matches[1]', 'query_vars' => 'my_path', ], 'template' => 'my-page', ] );
Same as above, but with multiple query vars, and with a callback instead of a template.
\WP_Path_Dispatch\Path_Dispatch()->add_path( [ 'path' => 'my-rewrite', 'rewrite' => [ 'rule' => 'my-path/([^/]+)/(.+)/?', 'redirect' => 'index.php?dispatch=my-rewrite&my_path=$matches[1]&my_section=$matches[2]', 'query_vars' => [ 'my_path', 'my_section' ], ], 'callback' => [ My_Singleton(), 'my_method' ], ] );
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
This project is actively maintained by Alley Interactive. Like what you see? Come work with us.
License
The GNU General Public License (GPL) license. Please see License File for more information.
alleyinteractive/wp-path-dispatch 适用场景与选型建议
alleyinteractive/wp-path-dispatch 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 168.13k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2023 年 02 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「alleyinteractive」 「wp-path-dispatch」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 alleyinteractive/wp-path-dispatch 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 alleyinteractive/wp-path-dispatch 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 alleyinteractive/wp-path-dispatch 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Ingest external feeds and other data sources into WordPress
Restrict and control access to the REST API
Interface to manage email newsletters
A library to migrate to a new WordPress theme incrementally.
A skeleton WordPress plugin
A skeleton WordPress theme
统计信息
- 总下载量: 168.13k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 8
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-or-later
- 更新时间: 2023-02-07