Last active
December 2, 2016 20:01
-
-
Save collegeman/dabe325e8267b3ca14df08bdd9f23eb0 to your computer and use it in GitHub Desktop.
What routing looks like in an Illuminated WordPress Plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// in your plugin's src/routes.php file | |
$router->rewrite('/some/arbitrary/url/{slug}', function($slug) { | |
// you can do anything here | |
update_option('option_name', $slug); | |
// if you return false, the request is over | |
//return false; | |
// if you return a string, WordPress will try to load a template by that name | |
return 'my-custom-template'; | |
}); | |
// you can also use Router::rewrite for more traditional rewrites to index.php | |
$router->rewrite('/some/other/url/{slug}', 'index.php?slug=$matches[1]'); | |
// and if you want to add to the REST API, that's easy too: | |
$router->get('/my/custom/endpoint', function() { | |
// you can do anything here | |
// and whatever you return will be transformed into JSON | |
return [ 'foo' => 'bar' ]; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment