Blog WordPress Function to Easily Include Files
July 6, 2014 Development, PHP, WordPress Social Share
In WordPress, I usually stick with the basic functions such as get_header(), get_footer() and get_sidebar() to include these core pieces. If you want to create your own puzzle pieces, WordPress has another function called get_template_part() that is just as useful.
This function allows you to include any file in the theme folder using only the page name without the extension. For example, I wanted to include a testimonial box that I placed in a file logically named include-testimonialbox.php. My personal preference is to prefix all my include files with include- to make them easier to find. Some people like to put them all in a subfolder.
Modular code like this makes updating the website much easier since now you only have to maintain one file instead of tracking down the different templates.
One caution I do have is that I often see developers overuse include files. Sometimes I’ll look at a template file and only see 10-15 include paths. Usually these developers don’t comment their code very well either so you’re still left digging through files looking for the right one to update.
For lots more information, Tom McFarlin wrote a great article on Tuts+ going over all the different ways to include files in WordPress.