Routes

Posted on November 25, 2016

The route system is an enhanced "if" statement. It is based on the WordPress template conditional tags.

A basic route is using a conditional tag and a closure callback.

Conditional tags

Here is the current list of available route conditional tags in alphabetical order:

  • 404
  • archive
  • attachment
  • author
  • category
  • date
  • day
  • front
  • home
  • month
  • page
  • paged
  • postTypeArchive
  • search
  • single
  • singular
  • sticky
  • subpage
  • tag
  • tax
  • template
  • time
  • year

Basic usage

Set a route for the WordPress Home page.

Simple text.

{
    "home" : "Home sweet home"
}

// Route: /
// Home sweet home

Constant

define("SOME_CONSTANT", "Home sweet home");
{
    "home" : "{{ SOME_CONSTANT }}"
}

// Route: /
// Home sweet home

Function

function homeSweetHome() {

   echo 'Home sweet home';
}
{
    "home" : "homeSweetHome"
}

// Route: /
// Home sweet home

Class method

class ModelPages
{
    public static function home() {
        echo 'Home sweet home';
    }
}
{
    "home" : ["\\MyProject\\ModelPages", "home"]
}
// Route: /
// Home sweet home

Class method from braces

class ModelPages
{
    public static function home() {
        echo 'Home sweet home';
    }
}
{
    "home" : "{{ \\MyProject\\ModelPages::home }}"
}
// Route: /
// Home sweet home

Add page template to WordPress

{
    "page-sometemplate" : {
        "html" : "{{ \\MyProject\\ModelPages::home }}",
        "template_name" : "Some template"
    }
}
// Route: /{page_with_template}
// Home sweet home

Page with custom route

{
    "custom_route" : {
        "html" : "{{ \\MyProject\\ModelPages::customRoute }}"
    }
}
// Route: /custom_route
// Here is custom route

Add css class to body_class()

{
    "custom_route" : {
        "html" : "{{ \\MyProject\\ModelPages::customRoute }}",
        "class" : "red-page"
    }
}
// Route: /custom_route
// 

Default config path

{{ Project dir }}/app/config/routes.json

You can rewrite this path by *lf_config_routes_path filter.

Keys

route_type (string) it can be native WordPress conditional tag, page template, taxonomy, page.

Item arguments

interpret_data (string | callable) route content. Example: {{ CONSTANT }}, {{ Class::method }}, ["Class", "method"], functionName.

OR

html (string | callable) (required) route content. Example: {{ CONSTANT }}, {{ Class::method }}, ["Class", "method"], functionName.

class (string) (optional) css class will be added to body_class function return value.

template_name (string) (opitonal) if you set this, route will be added to page templates dropdown in wordpress backend.

Based on

WordPress template conditional tags

1 Comments

  • Ella
    Ella
    January 3, 2017 at 11:43 am
    A million thanks for posting this intfomarion.
    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *