• Stars
    star
    188
  • Rank 204,990 (Top 5 %)
  • Language
    PHP
  • Created almost 10 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple routing for WordPress

Routes

Simple routing for WordPress. Designed for usage with Timber

Build Status Coverage Status Packagist Downloads

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!

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)

<?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)

Usage

A functions.php where I want to display custom paginated content:

<?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:

To paginate:

page/:pagenum

To edit a user:

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.

So in this example: 'info/:name/page/:pg', $params would have data for:

  • $data['name']
  • $data['pg']

... which you can use in the callback function as a part of your query


load

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

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!)

$template_params Any data you want to send to the resulting view. Example:

<?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)

$status_code Send an optional status code. Defaults to 200 for 'Success/OK'

More Repositories

1

jigsaw

Simple ways to make admin customizations for WordPress
PHP
156
star
2

jquery-total-storage

A jQuery plugin to manage local storage and cookies simultaneously in a simple interface
JavaScript
151
star
3

react-router-guards

Guard middleware for React Router navigation
TypeScript
130
star
4

skela-wp-theme

An Upstatement-flavored starter theme for WordPress
PHP
63
star
5

puppy

Starter kit and delivery system for building static prototypes with Twig
HTML
48
star
6

stream-manager

Curate streams of WordPress posts.
PHP
39
star
7

zoneboard

A Simple JSON-based Dashboard Replacement
CSS
30
star
8

style-guide-twig

Style Guide Template for Twig/Timber
HTML
29
star
9

quickshare

Simple way of implementing social media share buttons
JavaScript
26
star
10

ups-dock

A front-end stack for Docker-based projects at Upstatement
HTML
23
star
11

mesh

Bootstrap content into a WordPress site
PHP
16
star
12

snd_layout_editor

Homepage layout editor demo created during SND Makes: Boston
CSS
12
star
13

mobile-preview

WordPress plugin to preview the current view on a mobile device
PHP
12
star
14

eslint-config

Upstatement's ESLint Config
JavaScript
12
star
15

upbase

⛔️ DEPRECATED
CSS
11
star
16

ups-public-library

A knowledge base and learning tool for web designers who want to write more code.
SCSS
8
star
17

prettier-config

Upstatement's Prettier Config
Shell
7
star
18

generator-style-guide-twig

Yeoman Generator to quickly scaffold the Upstatement Style Guide for Twig/Timber
CSS
5
star
19

react-hooks

Repository of reusable custom React hooks
TypeScript
5
star
20

puppy-lib

An adorable library for building a static site generator
JavaScript
4
star
21

craft-starter

An Upstatement-flavored starter project for Craft 3
PHP
4
star
22

ups-editorial-wp-plugin

A plugin for customizing the editor experience to suit Upstatement-built WordPress projects.
JavaScript
3
star
23

ups-mixin-lib

Bespoke Sass mixin library from Upstatement
CSS
3
star
24

mobile-stories-wp-theme

A WordPress theme for exploring mobile story telling
PHP
2
star
25

sndMakes4

This is the SND Makes repo for Team 4.
PHP
2
star
26

craft-netlify-trigger

A Craft CMS plugin to allow content editors to trigger static site builds on Netlify.
PHP
2
star
27

presentations

Presentations for Upstatement
JavaScript
1
star
28

boston-built

A repository for the born-again bostonbuilt.org (Puppy)
HTML
1
star
29

rails_firebase_auth_demo

Ruby
1
star