makeitworkpress / wp-router

Enables developers to easily create custom routes with matching templates accordingly.
https://makeitwork.press/scripts/wp-router/
GNU General Public License v3.0
32 stars 6 forks source link
wordpress wordpress-development wordpress-library wordpress-php-library wordpress-routes

wp-router

Enables developers to add custom routes and templates to their WordPress theme. In other Words, you can easily add custom permalinks to your theme such as yourwebsite.dev/custom/ which in order point to a custom template.

WP Router is maintained by Make it WorkPress.

Usage

Include the WP-Router in your plugin, theme or child theme files. Require it in your functions.php file or use a PHP autoloader. You can read more about autoloading in the readme of wp-autoload.

Syntax

new MakeitWorkPress\WP_Router\Router(Array $routes, String $folder, String $query_var, Boolean $debug);

Create a new instance of WP_Router\Router

Create a new instance of the Router class with the array of routes as an argument in the format displayed below.

$router = new MakeitWorkPress\WP_Router\Router( 
    [
        'custom'    => ['route' => custom/, 'title' => __('Custom Template Title')],
        'another'   => ['route' => friedpizza/, 'title' => __('Fried Pizza!')]
    ], 
    'templates',    // The folder in your theme or child theme where the custom templates are stored. Use any full path to locate templates in plugins.
    'template',     // The query var by which the template is identified, in this case through get_query_var('template'). Defaults to template.
    false           // Whether to debug or not, which will output all rewrite rules on the front-end
);

Include template

Include the specific templates in your theme. In the case above, you need to have have a custom.php and an another.php template in the folder /templates/ in your parent or child theme. Obviously, this will be another folder if you changed the second argument.

Flush Permalinks

After adding new routes, do not forget to flush your permalinks. The easiest way to do is to head over to your permalink settings and save your settings. Please note that WP-Router only supports pretty permalinks.

Result

With the above example, you will have yourwebsite.dev/custom/ using templates/custom.php and yourwebsite.dev/friedpizza/ using templates/another.php