Why should I use get_template_directory() when include files?

When I include files in wordpress I noticed that include works fine without get_template_directory(). Include even works fine without parenteses.

include( get_template_directory() . '/includes/front/enqueue.php' );

works fine as well as

include '/includes/front/enqueue.php';

Could anyone please explain, should I use get_template_directory() when inlcude files in wordpress? And is it ok to not use parentheses after include?

Topic include functions directory Wordpress

Category Web


get_template_directory() can be filtered. This can be useful when a plugin wants to interact with your theme, for example when you want to write custom plugins to extend the theme without changing it.

For example:

add_filter( 'template_directory', function( $template_dir, $template, $theme_root ) {
    if ( some_condition( $theme_root ) ) {
        return plugin_dir_path( __FILE__ ) . 'template';
    }
});

include is an expression, not a function, so omitting parentheses is fine.

Also note that plugin_dir_path() has a trailing slash while get_template_directory() doesn't.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.