Unlock your potential with a Pro Membership
Here is where you propel your career forward. Come join us today. Learn more.
Initialization Tasks – Theme Constants
Lab: Part 1 – Introduction to Modularity – Developer’s Genesis Starter Child Theme
Video Runtime: 14:47
It’s time to splice out the initialization code and load it into our new Genesis starter theme’s init.php
file. The file loaders will go into the autoload.php
file, while the constant declarations go into the init.php
file.
The typical way of doing the theme constants is to hard-code them such as this:
define( 'CHILD_THEME_NAME', 'Genesis Sample' ); | |
define( 'CHILD_THEME_URL', 'http://www.studiopress.com/' ); | |
define( 'CHILD_THEME_VERSION', '2.2.4' ); |
Hum, but this information is already available in the theme’s style.css
file. And as a matter of fact, WordPress reads that information right out of the CSS file. You can access that same information and then use it to populate your theme constants like this:
<?php | |
/** | |
* Theme initialization | |
* | |
* @package KnowTheCode\Developers | |
* @since 1.0.0 | |
* @author hellofromTonya | |
* @link https://knowthecode.io | |
* @license GNU General Public License 2.0+ | |
*/ | |
namespace KnowTheCode\Developers; | |
/** | |
* Initialize the theme's constants. | |
* | |
* @since 1.0.0 | |
* | |
* @return void | |
*/ | |
function init_constants() { | |
$child_theme = wp_get_theme(); | |
define( 'CHILD_THEME_NAME', $child_theme->get( 'Name' ) ); | |
define( 'CHILD_THEME_URL', $child_theme->get( 'ThemeURI' ) ); | |
define( 'CHILD_THEME_VERSION', $child_theme->get( 'Version' ) ); | |
define( 'CHILD_TEXT_DOMAIN', $child_theme->get( 'TextDomain' ) ); | |
} | |
init_constants(); |
In this episode, I will walk you through how this works and why, as well as discuss why you want to use the built-in WordPress function wp_get_theme()
instead of hard-coding. It will save you time, effort, errors, and speed up your build time. Let’s get started.
You get WET when you swim. Stay DRY when you code.
Episodes
Total Lab Runtime: 03:59:03
- 1 Lab Introductionfree 05:01
- 2 What the Heck is Modularity?free 13:12
- 3 The Why of Clean Codefree 12:47
- 4 Laying Out the Theme's Architecture - Part 1pro 09:04
- 5 Laying Out the Theme's Architecture - Part 2pro 10:34
- 6 Slicing Up the Sample Theme's functions.phppro 06:44
- 7 Initialization Tasks - Theme Constantspro 14:47
- 8 Initialization Tasks - Directory Constantspro 05:49
- 9 Setup Taskspro 38:00
- 10 Refactoring Theme Supportspro 08:01
- 11 Refactoring Image Sizespro 03:52
- 12 Genesis Structural Componentspro 05:10
- 13 Load Assets (Enqueue)pro 05:56
- 14 Theme Customizer Filespro 15:43
- 15 Theme Settings Defaultspro 06:34
- 16 Autoload Modulespro 11:19
- 17 Unregister Default Genesis Callbackspro 00:00
- 18 Test & Wrap it Uppro 06:30