Craig Rowe

Techlead / Developer

March, 2012

Rule Based Themes - wordpress plugin

  • wordpress
  • php
  • rules
  • theming

The rule based themes plugin provides a mechanism to define rules that are applied via rbtme_get_theme() to return a theme string that can be used, for example, in a body class.

Rules are stored within rules.php (a file created/modified by the plugin when rules are saved). This file stores the rules as a PHP associative array with the file actually included at runtime avoiding any database queries when processing the final result.

Both the rules.php file and the '/cache' folder (which stores cached results from api calls) within the plugin directory must be writable by PHP. Similarly curl must be enabled on the server to allow the retrieval of remote xml.

rbtme_get_rule_list() can be used in templates if a listing of the currently available rules is required.

Supported Sub-Rule Types

  • Those based on dates or times (Day of week (1-7), Day (1-31), Month (1-12), Year (yyyy), Hour (0-23)
  • Those based on calls to public xml apis

A rule can be any combination of these sub-rule types in a boolean 'and' style. Boolean 'or' is achieved by having multiple rules with the same output value.

Possible Rules

  • A set of rules for day and night
  • A set of rules for specific times of year e.g. halloween or christmas
  • A set of rules based on weather information retrieved from a public xml api

Reference example

The reference example is the Headscape Barn site that uses it to swap out the Pixel Art header.

Rules include the use of the unofficial google weather api to render rain and snow.

Tested on

  • Wordpress 3.3.1
  • PHP 5

All portfolio content is copyright and cannot be reproduced without express persmission. These pages include both previously implemented and partially designed websites as well as university and exemplar projects.