How to Create and Manage External Files for A Child Theme
In “Beginner Introduction to WordPress Child Themes” I showed you the first basic step for setting your child theme by creating a new style sheet.
Another important step in this process is to setup the external files you will need to customize your child theme.
One of these important files is the functions.php file. This is where your central, or core, functionality is created. A good example of this is creating a custom post type that would be called out in the child theme functions.php file.
You can structure the setup of the function.php files however you want. If you build a child theme based on a particular custom post setup that you only want for a particular theme, then you can just create the new functions.php file for that specific theme. If you switch themes in WordPress, those custom post types will go away.
However, if you want to be able to use the custom post types for all your child themes, then you may want to consider creating a single php file with the custom functions you want, which can then be ‘included’ in the specific theme functions.php file.
I highly recommend you do it this way, because it’s much easier to make a one line of code change to a child theme if you, or your client, changes their mind.
Where do you put these new child theme files?
1. Create new files for added functionality, design, etc., for that specific theme directly into the child theme folder. An example would be to create a new functions.php file, which would contain the new custom post type php.
2. When you want to create and use custom functions across multiple themes, i.e custom post types, you will create and save this new ‘custompost.php file in the “themes” folder inside your site WordPress install. [Note: you can name the file whatever you want.]
Then you will create a new ‘functions.php’ file in the specific theme, that would have the ‘include’ function code for the ‘custompost.php’ file.
Remember, when you setup up the child theme styles.css folder in Part 1, you referenced the parent theme. When that child theme is installed and activated, WordPress will first load the parent theme, and then over-ride and add design and functionality from the added files in the child theme folder.
To continue with our custom post example, we created a ‘custompost.php’ file and saved it in the main theme directory. Then we created a new functions.php file and saved it in the child theme folder (wordpress main folder > wp-content > themes > child themes).
To add the ‘custompost.php’ to the new child theme, we insert the following code into the child theme ‘functions.php’ file:
include_once (ABSPATH . ‘wp-content/themes/custompost.php’);
The ‘Include’ function will pull the file referenced once. The ABSPATH, (absolute path), will tell WordPress to reference the absolute path to the ‘custompost.php’ file. If you create and install additional child themes, you only need to add this code snippet to use the ‘custompost.php’ file.
My goal in this article was to provide some ideas and recommendations on how to manage files for your child theme creations. Hope this helps, and thanks for reading.