PHPackages                             chameleon2die4/wp-router - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. chameleon2die4/wp-router

ActivePackage[HTTP &amp; Networking](/categories/http)

chameleon2die4/wp-router
========================

Routes paths to callback functions in WordPress.

1.0(4y ago)11.0kMITPHPPHP &gt;=7.2.0

Since Nov 8Pushed 4y agoCompare

[ Source](https://github.com/Chameleon2die4/WP-Router)[ Packagist](https://packagist.org/packages/chameleon2die4/wp-router)[ Docs](https://github.com/Chameleon2die4/WP-Router)[ RSS](/packages/chameleon2die4-wp-router/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (5)Used By (0)

WP Router
=========

[](#wp-router)

[![PHP Composer](https://github.com/Chameleon2die4/WP-Router/actions/workflows/php.yml/badge.svg)](https://github.com/Chameleon2die4/WP-Router/actions/workflows/php.yml)[![Latest Version](https://camo.githubusercontent.com/c3c6a8c6f2935c6c6d77d82e187b6324eb5092e3b642fc6cc45338e5d7aa245e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f7461672f4368616d656c656f6e32646965342f57502d526f757465723f736f72743d73656d766572266c6162656c3d76657273696f6e)](https://github.com/Chameleon2die4/WP-Router/)[![Packagist](https://camo.githubusercontent.com/8e15bd3f3c7963753925df222f375afa35f0c45284527a0ecbf7a02ad341fdd3/68747470733a2f2f62616467656e2e6e65742f7061636b61676973742f762f6368616d656c656f6e32646965342f77702d726f757465722f6c6174657374)](https://packagist.org/packages/chameleon2die4/wp-router/)[![PHP Version Require](https://camo.githubusercontent.com/74083903099b64417484643ec16eddc12c4a9dd1a2a1182fb6b4209df07c639d/68747470733a2f2f62616467656e2e6e65742f7061636b61676973742f7068702f6368616d656c656f6e32646965342f77702d726f757465722f)](https://www.php.net/docs.php)[![License](https://camo.githubusercontent.com/5e25f857ea70d6e61023befad1d4195a9196c47ce3e73e4ef77a0d8e2e2e98f3/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d6d69742d626c75652e737667)](https://github.com/Chameleon2die4/WP-Router/blob/master/LICENSE.md)

Provides a simple API for mapping requests to callback functions.

Description
-----------

[](#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
------------

[](#installation)

### Composer:

[](#composer)

Browse into directory and run;

```
$ composer require chameleon2die4/wp-router
```

### Requirements:

[](#requirements)

- [PHP](http://php.net/manual/en/install.php) &gt;= 7.0

### Uninstall

[](#uninstall)

After remove package use `flush_rewrite_rules()` function.

Usage
-----

[](#usage)

### Creating Routes

[](#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

[](#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
-------------------

[](#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

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 72.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~276 days

Total

4

Last Release

1548d ago

Major Versions

0.8 → 1.02022-02-13

PHP version history (2 changes)0.6PHP &gt;=5.2.0

0.7PHP &gt;=7.2.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/1823d22814fc8bbce2e19dcc645c706f69be36435fc5105ba74492d0d064b943?d=identicon)[Chameleon2die4](/maintainers/Chameleon2die4)

---

Top Contributors

[![jbrinley](https://avatars.githubusercontent.com/u/288845?v=4)](https://github.com/jbrinley "jbrinley (40 commits)")[![Chameleon2die4](https://avatars.githubusercontent.com/u/3635535?v=4)](https://github.com/Chameleon2die4 "Chameleon2die4 (10 commits)")[![cliffordp](https://avatars.githubusercontent.com/u/1812179?v=4)](https://github.com/cliffordp "cliffordp (4 commits)")[![borkweb](https://avatars.githubusercontent.com/u/430385?v=4)](https://github.com/borkweb "borkweb (1 commits)")

---

Tags

wordpressrouter

### Embed Badge

![Health badge](/badges/chameleon2die4-wp-router/health.svg)

```
[![Health](https://phpackages.com/badges/chameleon2die4-wp-router/health.svg)](https://phpackages.com/packages/chameleon2die4-wp-router)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
