Using WordPress ‘get_template_directory()’ PHP function

The get_template_directory() WordPress PHP function retrieves the template directory path for the active theme.

Usage

include( get_template_directory() . '/includes/my_file.php' );

Parameters

  • None

More information

See WordPress Developer Resources: get_template_directory()

Examples

Loading a Custom CSS File

Load a custom CSS file from your theme’s directory:

function load_custom_css() {
    wp_enqueue_style('custom-style', get_template_directory_uri() . '/css/custom.css');
}
add_action('wp_enqueue_scripts', 'load_custom_css');

Including a Template Part

Include a template part from your theme’s directory:

// Load the header template part
get_template_part('template-parts/header', 'main');

Requiring a Functions File

Require a functions file from your theme’s directory:

// Require the theme functions file
require_once get_template_directory() . '/inc/theme-functions.php';

Registering a Custom Sidebar

Register a custom sidebar for your theme:

function my_custom_sidebar() {
    register_sidebar( array(
        'name' => 'My Custom Sidebar',
        'id' => 'my_custom_sidebar',
        'description' => 'A custom sidebar for my theme',
        'before_widget' => '<div id="%1$s" class="widget %2$s">',
        'after_widget' => '</div>',
        'before_title' => '<h2 class="widgettitle">',
        'after_title' => '</h2>',
    ) );
}
add_action('widgets_init', 'my_custom_sidebar');

Creating a Custom Navigation Menu

Create a custom navigation menu for your theme:

function register_my_menu() {
    register_nav_menu('header-menu', __('Header Menu'));
}
add_action('init', 'register_my_menu');