PHPackages                             alleyinteractive/wp-path-dispatch - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. alleyinteractive/wp-path-dispatch

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

alleyinteractive/wp-path-dispatch
=================================

Simply and easily add a URL which fires an action, triggers a callback, and/or loads a template.

v1.1.0(8mo ago)8177.8k↓30.1%1[1 PRs](https://github.com/alleyinteractive/wp-path-dispatch/pulls)GPL-2.0-or-laterPHPPHP ^8.2CI passing

Since Feb 7Pushed 1w ago19 watchersCompare

[ Source](https://github.com/alleyinteractive/wp-path-dispatch)[ Packagist](https://packagist.org/packages/alleyinteractive/wp-path-dispatch)[ Docs](https://github.com/alleyinteractive/wp-path-dispatch)[ RSS](/packages/alleyinteractive-wp-path-dispatch/feed)WikiDiscussions develop Synced 2d ago

READMEChangelog (3)Dependencies (4)Versions (5)Used By (0)

WP Path Dispatch
================

[](#wp-path-dispatch)

[![All Pull Request Tests](https://github.com/alleyinteractive/wp-path-dispatch/actions/workflows/all-pr-tests.yml/badge.svg?branch=develop)](https://github.com/alleyinteractive/wp-path-dispatch/actions/workflows/all-pr-tests.yml)

Simply and easily add a URL which fires an action, triggers a callback, and/or loads a template.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require alleyinteractive/wp-path-dispatch
```

Usage
-----

[](#usage)

At any point before init,

```
\WP_Path_Dispatch\Path_Dispatch()->add_path(
	[
		'path'     => 'some-path',
		'callback' => 'some_function'
	]
);
```

This will cause  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()](http://codex.wordpress.org/Rewrite_API/add_rewrite_rule)for details about 'rule', 'redirect' (rewrite), and 'position'.

Full breakdown of all the path options
--------------------------------------

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

[](#examples)

Simplest possible usages: fires the action 'dispatch\_path\_my-path' at

```
\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

```
\WP_Path_Dispatch\Path_Dispatch()->add_path(
	[
		'path'     => 'my-path',
		'callback' => 'my_function',
	]
);
```

Load the template file 'dispatch-custom-page.php' at

```
\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.  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
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Credits
-------

[](#credits)

This project is actively maintained by [Alley Interactive](https://github.com/alleyinteractive). Like what you see? [Come work with us](https://alley.co/careers/).

- [Matt Boynes](https://github.com/mboynes)
- [All Contributors](../../contributors)

License
-------

[](#license)

The GNU General Public License (GPL) license. Please see [License File](LICENSE) for more information.

###  Health Score

54

—

FairBetter than 96% of packages

Maintenance82

Actively maintained with recent releases

Popularity41

Moderate usage in the ecosystem

Community17

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 66.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 ~496 days

Total

3

Last Release

249d ago

PHP version history (2 changes)v1.0.0PHP ^8.0

v1.1.0PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/338d27065b1074f2d66d049d742f22996dd137eef6f91bc8f75350ceee1e8ef2?d=identicon)[srtfisher](/maintainers/srtfisher)

---

Top Contributors

[![srtfisher](https://avatars.githubusercontent.com/u/346399?v=4)](https://github.com/srtfisher "srtfisher (22 commits)")[![Copilot](https://avatars.githubusercontent.com/in/1143301?v=4)](https://github.com/Copilot "Copilot (5 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (3 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (2 commits)")[![dlh01](https://avatars.githubusercontent.com/u/697432?v=4)](https://github.com/dlh01 "dlh01 (1 commits)")

---

Tags

wordpresswordpress-packagealleyinteractivewp-path-dispatch

### Embed Badge

![Health badge](/badges/alleyinteractive-wp-path-dispatch/health.svg)

```
[![Health](https://phpackages.com/badges/alleyinteractive-wp-path-dispatch/health.svg)](https://phpackages.com/packages/alleyinteractive-wp-path-dispatch)
```

###  Alternatives

[alleyinteractive/wp-block-converter

Convert HTML into Gutenberg Blocks with PHP

65422.1k2](/packages/alleyinteractive-wp-block-converter)[alleyinteractive/wp-curate

Plugin to curate homepages and other landing pages

11234.6k](/packages/alleyinteractive-wp-curate)[alleyinteractive/wp-alleyvate

Defaults for WordPress sites by Alley.

3442.6k](/packages/alleyinteractive-wp-alleyvate)[alleyinteractive/feed-consumer

Ingest external feeds and other data sources into WordPress

116.3k](/packages/alleyinteractive-feed-consumer)[alleyinteractive/wp-bulk-task

A library to assist with running performant bulk tasks against WordPress objects.

21425.7k5](/packages/alleyinteractive-wp-bulk-task)

PHPackages © 2026

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