2017 © Pedro Peláez
 

library routes

Manage rewrites and routes in WordPress with this dead-simple plugin

image

upstatement/routes

Manage rewrites and routes in WordPress with this dead-simple plugin

  • Wednesday, January 24, 2018
  • by jarednova
  • Repository
  • 27 Watchers
  • 84 Stars
  • 217,321 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 12 Forks
  • 4 Open issues
  • 5 Versions
  • 12 % Grown

The README.md

Routes

Simple routing for WordPress. Designed for usage with Timber, (*1)

Build Status Coverage Status Packagist Downloads, (*2)

Basic Usage

/* functions.php */
Routes::map('myfoo/bar', 'my_callback_function');
Routes::map('my-events/:event', function($params) {
    $event_slug = $params['event'];
    $event = new ECP_Event($event_slug);
    $query = new WPQuery(); //if you want to send a custom query to the page's main loop
    Routes::load('single.php', array('event' => $event), $query, 200);
});

Using routes makes it easy for you to implement custom pagination — and anything else you might imagine in your wildest dreams of URLs and parameters. OMG so easy!, (*3)

Some examples

In your functions.php file, this can be called anywhere (don't hook it to init or another action or it might be called too late), (*4)

<?php
Routes::map('blog/:name', function($params){
    $query = 'posts_per_page=3&post_type='.$params['name'];
    Routes::load('archive.php', null, $query, 200);
});

Routes::map('blog/:name/page/:pg', function($params){
    $query = 'posts_per_page=3&post_type='.$params['name'].'&paged='.$params['pg'];
    $params = array('thing' => 'foo', 'bar' => 'I dont even know');
    Routes::load('archive.php', $params, $query);
});

map

Routes::map($pattern, $callback), (*5)

Usage

A functions.php where I want to display custom paginated content:, (*6)

<?php
Routes::map('info/:name/page/:pg', function($params){
    //make a custom query based on incoming path and run it...
    $query = 'posts_per_page=3&post_type='.$params['name'].'&paged='.intval($params['pg']);

    //load up a template which will use that query
    Routes::load('archive.php', null, $query);
});

Arguments

$pattern (required) Set a pattern for Routes to match on, by default everything is handled as a string. Any segment that begins with a : is handled as a variable, for example:, (*7)

To paginate:, (*8)

page/:pagenum

To edit a user:, (*9)

my-users/:userid/edit

$callback A function that should fire when the pattern matches the request. Callback takes one argument which is an array of the parameters passed in the URL., (*10)

So in this example: 'info/:name/page/:pg', $params would have data for: * $data['name'] * $data['pg'], (*11)

... which you can use in the callback function as a part of your query, (*12)


load

Routes::load($php_file, $args, $query = null, $status_code = 200), (*13)

Arguments

$php_file (required) A PHP file to load, in my experience this is usually your archive.php or a generic listing page (but don't worry it can be anything!), (*14)

$template_params Any data you want to send to the resulting view. Example:, (*15)

<?php
/* functions.php */

Routes::map('info/:name/page/:pg', function($params){
    //make a custom query based on incoming path and run it...
    $query = 'posts_per_page=3&post_type='.$params['name'].'&paged='.intval($params['pg']);

    //load up a template which will use that query
    $params['my_title'] = 'This is my custom title';
    Routes::load('archive.php', $params, $query, 200);
});
<?php
/* archive.php */

global $params;
$context['wp_title'] = $params['my_title']; // "This is my custom title"
/* the rest as normal... */
Timber::render('archive.twig', $context);

$query The query you want to use, it can accept a string or array just like Timber::get_posts -- use the standard WP_Query syntax (or a WP_Query object too), (*16)

$status_code Send an optional status code. Defaults to 200 for 'Success/OK', (*17)

The Versions

24/01 2018

dev-master

9999999-dev http://routes.upstatement.com

Manage rewrites and routes in WordPress with this dead-simple plugin

  Sources   Download

MIT

The Requires

 

The Development Requires

rewrite routing routes redirects

06/07 2016

0.4

0.4.0.0 http://routes.upstatement.com

Manage rewrites and routes in WordPress with this dead-simple plugin

  Sources   Download

MIT

The Requires

 

The Development Requires

rewrite routing routes redirects

07/03 2015

0.3

0.3.0.0 http://routes.upstatement.com

Manage rewrites and routes in WordPress with this dead-simple plugin

  Sources   Download

MIT

The Requires

 

The Development Requires

rewrite routing routes redirects

17/02 2015

0.2

0.2.0.0 http://routes.upstatement.com

Manage rewrites and routes in WordPress with this dead-simple plugin

  Sources   Download

MIT

The Requires

 

The Development Requires

rewrite routing routes redirects

17/02 2015

0.1

0.1.0.0 http://routes.upstatement.com

Manage rewrites and routes in WordPress with this dead-simple plugin

  Sources   Download

MIT

The Requires

 

The Development Requires

rewrite routing routes redirects