I’m going to take a brief moment to explain a seriously cool idea that came to light while building Whitelabel Framework theme. When building a frame you have to approach everything you do with the questions “how could others customize this in any way possible”. Well, this is why WordPress invented the do_action / add_action system.
So first we looked at the themes 2010 and 2011 and read through every line. From this we learned some tips and tricks they were using to “queue” different parts of the theme to be loaded. From there we developed a way to “enable” developers to add and modify different parts of the theme right from the functions file or plugin. Furthermore, we continued to build all the “standard” default theme pages like header.php and footer.php. Eventually we built what you see in the Whitelabel Framework theme, which is a set of CSS frameworks (select-able) that uses to action/filter system to move between frameworks.
This didn’t just happen in one weekend. We developed the framework over years of using it alongside child themes on production websites and real businesses! So how did we know when we were successfully creating a theme framework? Answer: When our child themes went from 20-30 PHP files each to 3-6 PHP files each, this is when we knew it was working. At that point we released it to the public at version 1.0.
The problem
End developers need a way to stop, override, append, or prepend any of the different parts and sections of the webpage templating system. Just calling “get_template_part(‘file_here’)” was not dynamic. It forced some file to be loaded instead of queueing up the file to be loaded.
Modifying which file was loaded in that statement required overriding the entire php file with a new one in the child theme! Undesirable. Why can’t we just dequeue the file from being loaded and queue up another one… just like we do with stylesheets and javascript? Now you can.
The solution
With 2 simple lines of PHP code, you can “queue” up a file to be included instead of requiring that the file be included.
Line 1: Adding this line of code to one of your WordPress pages, like index.php will create the placeholder where files can be “included”. I used included because get_template_part is like a PHP include() function, its just smarter.
<?php do_action('build_theme_header_nav', 'part.header', 'navigation.inc'); ?>
This function has 3 parts. ‘build_theme_header_nav’ is the name of the action, part.header is the first string parameter and navigation.inc is the second string. The 2nd and 3rd paramters are passed into the second line of text you will build.
Line 2: Adding this line of code to your themes functions.php file is the best practice here. Later, if you build a lot of these, you may move it into a theme-config.php file.
<?php add_action('build_theme_header_nav', 'get_template_part', 10, 2); ?>
This function has 4 parts. ‘build_theme_header_nav’ is the name of the action (note it matches the do action which is what connects them). ‘get_template_part’ is the name of the php function we want to call. 10 is the priority in the queue, and 2 is the number of parameters we are sending (note we had 2 parameters above).
Summery: So line 1 creates a placeholder where actions can take place. Imagine that its your “cursor” and any actions will create content at that placeholder. Line 2 then says this in lamen terms “during the action build_theme_header_nav, get the template file associated with it which is part.header-navigation.inc.php.
WordPress References
Whitelabel Framework – files with examples:
© Copyright 2023 Sethmatics Websites All Rights Reserved.