wordpress-phoenix / abstract-theme-base
PHP class to extend when building a WordPress theme allowing you to follow smart theme setup standards.
Requires (Dev)
- automattic/vipwpcs: ^0.4.0
- dealerdirect/phpcodesniffer-composer-installer: ^0.5.0
- wp-coding-standards/wpcs: ^1.2.0
This package is auto-updated.
Last update: 2024-11-06 16:06:27 UTC
README
Used as a base class to help standardize the way we build WordPress themes.
Table of Contents
Installation
You can use this library to start a new theme from scratch, or you can enhance your existing themes with this library. Once you have read over the installation instructions it should make sense which direction to go.
Composer style (recommended)
- Confirm that composer is installed in your development environment using
which composer
. If CLI does not print any path, you need to install Composer. - Set CLI working directory to wp-content/themes/{your-theme-name}
- Install Abstract_Theme class via composer command line like
composer require wordpress-phoenix/abstract-theme-base
- Look at sample code below to see how to include this library in your theme.
Manual Installation
- Download the most updated copy of this repository from
https://api.github.com/repos/WordPress-Phoenix/abstract-theme-base/zipball
- Extract the zip file, and copy the PHP file into your theme project.
- Include the file in your theme.
Usage
Why should you use this library when building your theme?
By building your theme using OOP principals, and extending this Theme_Base class object, you will be able to quickly and efficiently build your theme, allowing it to be simple to start, but giving it the ability to grow complex without changing its architecture.
Immediate features include:
- Built in SPL Autoload for your includes folder, should you follow WordPress codex naming standards for class files.
- Template class provides you all the best practices for standard theme initialization
- Minimizes code needed / maintenance of your main theme file.
- Assists developers new to WordPress theme development in file / folder architecture.
- By starting all your themes with the same architecture, we create a standard that is better for the dev community.
Simplest example of the main theme functions file, and required theme class file
custom-my-theme.php
:
<?php // Avoid direct calls to this file, because now WP core and framework has been used if ( ! function_exists( 'add_filter' ) ) { header( 'Status: 403 Forbidden' ); header( 'HTTP/1.1 403 Forbidden' ); exit(); } $current_dir = trailingslashit( dirname( __FILE__ ) ); /** * 3RD PARTY DEPENDENCIES * (manually include_once dependencies installed via composer for safety) */ if ( ! class_exists( 'WPAZ_Theme_Base\\V_2_6\\Abstract_Plugin' ) ) { include_once $current_dir . 'class-abstract-theme.php'; } // Maybe load vendor director from theme, if it exists. if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) { include_once( __DIR__ . '/vendor/autoload.php' ); } /** * Load in the main theme application and initialize it. */ include_once $current_dir . 'app/class-theme.php'; Mynamespace\Custom_Theme\Theme::run( __FILE__ );
app/class-theme.php
:
<?php namespace Custom\My_theme; use WPAZ_Theme_Base\V_2_6\Abstract_Theme; /** * Class App */ class Theme extends Abstract_Theme { public static $autoload_class_prefix = __NAMESPACE__; // Set to 2 when you use 2 namespaces in the main app file public static $autoload_ns_match_depth = 2; public function onload( $instance ) { // Nothing yet } public function init() { do_action( static::class . '_before_init' ); // Do theme stuff usually looks something like // $subclass = new OptionalAppSubfolder\Custom_Class_Subclass(); // $subclass->custom_theme_function(); do_action( static::class . '_after_init' ); } public function authenticated_init() { if ( ! is_user_logged_in() ) { return; } // Ready for wp-admin - but not required //$this->admin = new Admin\App( $this ); } protected function defines_and_globals() { // None yet. } }