定制 chameleon2die4/wp-router 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

chameleon2die4/wp-router

Composer 安装命令:

composer require chameleon2die4/wp-router

包简介

Routes paths to callback functions in WordPress.

README 文档

README

PHP Composer Latest Version Packagist PHP Version Require License

Provides a simple API for mapping requests to callback functions.

Description

WordPress's rewrite rules and query variables provide a powerful system for mapping URL strings to collections of posts. Every request is parsed into query variables and turned into a SQL query via $wp_query->query().

Sometimes, though, you don't want to display a list of posts. You just want a URL to map to a callback function, with the output displayed in place of posts in whatever theme you happen to be using.

That's where WP Router comes in. It handles all the messy bits of registering post types, query variables, rewrite rules, etc., and lets you write code to do what you want it to do. One function call is all it takes to map a URL to your designated callback function and display the return value in the page.

Installation

Composer:

Browse into directory and run;

$ composer require chameleon2die4/wp-router

Requirements:

Uninstall

After remove package use flush_rewrite_rules() function.

Usage

Creating Routes

  • Your plugin should hook into the wp_router_generate_routes action. The callback should take one argument, a WP_Router object.
  • Register a route and its callback using WP_Router::add_route( $id, $args )
    • $id is a unique string your plugin should use to identify the route
    • $args is an associative array, that sets the following properties for your route. Any omitted argument will use the default value.
      • path (required) - A regular expression to match against the request path. This corresponds to the array key you would use when creating rewrite rules for WordPress.

      • query_vars - An associative array, with the keys being query vars, and the values being explicit strings or integers corresponding to match in the path regexp. Any query variables included here will be automatically registered.

      • title - The title of the page.

      • title_callback - A callback to use for dynamically generating the title. Defaults to __(). If NULL, the title argument will be used as-is. if page_callback or access_callback returns FALSE, title_callback will not be called.

        title_callback can be either a single callback function or an array specifying callback functions for specific HTTP methods (e.g., GET, POST, PUT, DELETE, etc.). If the latter, the default key will be used if no other keys match the current request method.

      • title_arguments - An array of query variables whose values will be passed as arguments to title_callback. Defaults to the value of title. If an argument is not a registered query variable, it will be passed as-is.

      • page_callback (required) - A callback to use for dynamically generating the contents of the page. The callback should either echo or return the contents of the page (if both, the returned value will be appended to the echoed value). If FALSE is returned, nothing will be output, and control of the page contents will be handed back to WordPress. The callback will be called during the parse_request phase of WordPress's page load. If access_callback returns FALSE, page_callback will not be called.

        page_callback can be either a single callback function or an array specifying callback functions for specific HTTP methods (e.g., GET, POST, PUT, DELETE, etc.). If the latter, the default key will be used if no other keys match the current request method.

      • page_arguments - An array of query variables whose values will be passed as arguments to page_callback. If an argument is not a registered query variable, it will be passed as-is.

      • access_callback - A callback to determine if the user has permission to access this page. If access_arguments is provided, default is current_user_can, otherwise default is TRUE. If the callback returns FALSE, anonymous users are redirected to the login page, authenticated users get a 403 error.

        access_callback can be either a single callback function or an array specifying callback functions for specific HTTP methods (e.g., GET, POST, PUT, DELETE, etc.). If the latter, the default key will be used if no other keys match the current request method.

      • access_arguments - An array of query variables whose values will be passed as arguments to access_callback. If an argument is not a registered query variable, it will be passed as-is.

      • template - An array of templates that can be used to display the page. If a path is absolute, it will be used as-is; relative paths allow for overrides by the theme. The string $id will be replaced with the ID of the route. If no template is found, fallback templates are (in this order): route-$id.php, route.php, page-$id.php, page.php, index.php. If FALSE is given instead of an array, the page contents will be printed before calling exit() (you can also accomplish this by printing your output and exiting directly from your callback function).

Example:

$router->add_route('wp-router-sample', array(
    'path' => '^wp_router/(.*?)$',
    'query_vars' => array(
        'sample_argument' => 1,
    ),
    'page_callback' => array(get_class(), 'sample_callback'),
    'page_arguments' => array('sample_argument'),
    'access_callback' => TRUE,
    'title' => 'WP Router Sample Page',
    'template' => array('sample-page.php', dirname(__FILE__).DIRECTORY_SEPARATOR.'sample-page.php')
));

In this example, the path http://example.com/wp_router/my_sample_path/ will call the function sample_callback in the calling class. The value of the sample_argument query variable, in this case "my_sample_path", will be provided as the first and only argument to the callback function. If the file sample-page.php is found in the theme, it will be used as the template, otherwise sample-page.php in your plugin directory will be used (if that's not found either, fall back to route-wp-router-sample.php, etc.).

Editing Routes

You can hook into the wp_router_alter_routes action to modify routes created by other plugins. The callback should take one argument, a WP_Router object.

Public API Function

Creating or changing routes should always occur in the context of the wp_router_generate_routes or wp_router_alter_routes actions, using the WP_Router object supplied to your callback function.

  • WP_Router::edit_route( string $id, array $changes ) - update each property given in $changes for the route with the given ID. Any properties not given in $changes will be left unaltered.
  • WP_Router::remove_route( string $id ) - delete the route with the given ID
  • WP_Router::get_route( string $id ) - get the WP_Route object for the given ID
  • WP_Router::get_url( string $id, array $arguments ) - get the URL to reach the route with the given ID, with the given query variables and their values
  • WP_Route::get( string $property ) - get the value of the specified property for the WP_Route instance

chameleon2die4/wp-router 适用场景与选型建议

chameleon2die4/wp-router 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.01k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 02 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「wordpress」 「router」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 chameleon2die4/wp-router 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 chameleon2die4/wp-router 我们能提供哪些服务?
定制开发 / 二次开发

基于 chameleon2die4/wp-router 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.01k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 5
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 26
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-02-13