duncanjbrown / plain-route
Define arbitrary endpoints in WordPress
Installs: 1 112
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/duncanjbrown/plain-route
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2025-10-26 00:56:02 UTC
README
This library provides a simple interface for WordPress's fiddly rewrite rules API.
It's useful for creating arbitrary endpoints with callbacks. For example, a webhook receiver for a payment gateway.
Usage
Creating an endpoint is accomplished by newing one up. You should do this on init.
add_action( 'init', function() {
new Plain_Route( 'stripe(/)?', [
'rewrite' => 'p=123',
'pre_get_posts' => function( $query ) {
if( $query->is_main_query() ) {
$query->set('stripe', true);
}
}]);
});
In the above example, pre_get_posts could be replaced with wp_title or wp. Or you could add them alongside.
You can also render any template with the special template callback.
Creating an endpoint that renders a specific template:
add_action( 'init', function() {
new Plain_Route( 'my-special-template(/)?', [
'template' => 'my-special-template.php'
]);
});
Credit
This class is a much-simplified variation of HM_Rewrite by humanmade.