Skip to content

PHP Guide

James Macon edited this page Jun 6, 2024 · 5 revisions

Theme PHP

The ThinkWP Starter Theme is build with Timber and Twig on top of traditional WordPress tooling. Bringing Twig templates in to WP allows template markup to be separated from theme logic; as such the directory structure is built around that as an organizing principle.

Structure

Overall WordPress's template hierarchy has been maintained. At the top level of the theme folder you'll find php files containing template rendering logic, theme functions, and helper classes. The difference is comes in how those are deployed on load.

functions.php

As Timber is written as a class that's meant to be extended by a theme, theme logic and functions have been moved to a class that's instantiated in functions.php. That class and supporting files live in the lib directory.

class-theme.php

The main file where the Theme class extends Timber\Site class. The constructor for the class is where you can add WordPress actions, filters, and hooks that reference methods defined in the body of the class using the following syntax:

add_action( 'wp_hook_name', [ $this, 'name_of_method' ], $args... );

Further down in the Theme class:

public function name_of_method( $args ) {
   ...
}

If you need to reference a function inside a twig template you can add it to the timber context as well you can add values to the global timber context available to all twig templates, see menu example.

For complete documentation see our Twig in WordPress doc.

wp-template-file.php

All template files live in the top level of the theme. The structure and naming follows WordPress's template hierarchy. Each file is used to setup individual contexts for and then render a Twig template file that contains template markup for the template. For more info see the index.phg example.

Custom Page Templates

If you need to create a page template for the project you can do so in the templates directory following along with timber rendering conventions.

Clone this wiki locally