How to Include Files From Your Theme Directory in WordPress

Home » Snippets » How to Include Files From Your Theme Directory in WordPress

Created with:



Creator: WPTurbo Team

Customize with WPTurbo AI

Add Snippet To Project

New Project
Add To Existing Project

Are you a WordPress developer looking to include files from your theme directory, but not sure where to start? By understanding the structure of WordPress and the hierarchy of its theme system, you can easily include any file from your theme directory. This not only helps in making your website dynamic but also improves your site’s organization and structure. In this article, we will be guiding you on how to retrieve and include files from your WordPress theme directory. Get ready to enhance your development skills and take control of your website’s content inclusion.

					function wpturbo_include_file_from_theme_dir() {
    include(get_template_directory() . '/custom-file.php');
add_action("init","wpturbo_include_file_from_theme_dir" );

The first order of business is to declare a fresh function – wpturbo_include_file_from_theme_dir(). As the name suggests, this function acts as a hotkey to include a customized file from a theme’s directory.

Inside this function, we have the include statement, which is bundled with the get_template_directory() function. This is followed by the specific path to the targeted file, in this case, it’s ‘/custom-file.php’.

include(get_template_directory() . '/custom-file.php');

It’s worth noting that the file path is relative to the root of the WordPress installation and should not include a leading slash unless you specifically want to denote an absolute path. To avoid confusion, always ensure that the directory and file references are correct.

The get_template_directory() function retrieves the absolute path to the directory of the current theme, eliminating the need to manually specify the theme’s directory. Subsequently, the concatenation operator (.) appends the specified file path i.e., ‘/custom-file.php’ to the directory path, forming a complete directory and file path for the include function to ingest from the theme’s directory. The result is the execution of the specified PHP file (custom-file.php, in this case) within the context of the script that fired this function, allowing its variables, classes, functions, constants, etc., to be available as if they were defined in the calling script itself.

Lastly, we’ve added an add_action() function at the bottom, which hooks the newly created function to the ‘init’ WordPress action hook. That implies, wpturbo_include_file_from_theme_dir() will be fired during the initialization of WordPress, and thus, the customized file will get included each time WordPress initializes.

add_action("init","wpturbo_include_file_from_theme_dir" );

In brief, this piece of code is a handy tool when you intend to include theme specific PHP files in WordPress.

Register an account to save your snippets or go Pro to get more features.