Hooks & Includes
We've gone over how to approach adding an external library to your theme, but what if you need to add some custom PHP?
Depending on what you're needing to include you can approach this a few different ways, each one accepted as part of our standardized build process.
Including single PHP files
Lets use a Bootstrap Navwalker for example. This particular navwalker adds all of the appropriate css classes and ID's to a standard WordPress menu. In turn this allows you to transform any WordPress menu into a Bootstrap Nav or Navbar.
This particular navwalker is a single PHP file. So where do we put it? Simple, we place it in the themes inc/
folder. This folder is already included with Underscores and contains a handful of other PHP includes. We're simply adding to these so we'll put our navwalker PHP file in the same folder.
Once the custom PHP file has been added, next all we need to do is require that file in our functions.php file. If you open up the functions.php file you'll notice a handful of other includes at the bottom of the file. We'll follow this pattern and simply add our new file to the bottom like so:
require get_template_directory() . '/inc/class-wp-bootstrap-navwalker.php';
You're now ready to use the WP Bootstrap Navwalker in your theme.
Custom Post Types & Custom Taxonomies
According to the WordPress documentation they now recommend registering all custom post types and custom taxonomies using a plugin. We do understand why they prefer developers to take this approach, however, because we are building a custom theme from scratch and it's unlikely this theme will ever be replaced, it is acceptable to register these directly within the theme. If you prefer to register these within a Plugin that is also acceptable.
If registering a custom post types and taxonomies within a theme you'll want to set up a new file for each (custom-post-types.php and custom-taxonomies.php). Both of these files will live in the inc/
folder. Remember from above, this is where most of our custom PHP files will go. Once the files have been added you'll once again add a require stament for each of these in the functions.php file. They would look something like this:
require get_template_directory() . '/inc/custom-post-types.php';
require get_template_directory() . '/inc/custom-taxonomies.php';
*Always be sure to include a comment above each statement briefly describing it's purpose. You can use the ones already there as a template.
Additional Custom PHP
If you need to add your own custom PHP living outside of a template or custom block file you can follow the same pattern as above and place that file in the /inc
folder, then require the same file in the functions.php
file.
When adding custom any custom PHP it's important to remember you already have a template-fucntions.php
and template-tags.php
file included in the Underscores theme. These files live in the inc/
folder and are already being required in the functions.php
file. Before adding your own custom PHP file, think about whether or not the code you need to add can simply be added to one of those files. If so, great! There's no need to add another PHP file if your code can live is one that's already being included with the theme.
Last updated