18/01
2016
Define arbitrary endpoints in WordPress
This library provides a simple interface for WordPress's fiddly rewrite rules API., (*1)
It's useful for creating arbitrary endpoints with callbacks. For example, a webhook receiver for a payment gateway., (*2)
Creating an endpoint is accomplished by new
ing one up. You should do this on init
., (*3)
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., (*4)
You can also render any template with the special template
callback., (*5)
Creating an endpoint that renders a specific template:, (*6)
add_action( 'init', function() { new Plain_Route( 'my-special-template(/)?', [ 'template' => 'my-special-template.php' ]); });
This class is a much-simplified variation of HM_Rewrite by humanmade., (*7)