This little “PHP include” guide, will show you how to include a PHP-file in your WordPress theme (using a relative path). This snippet isn’t so much a “WordPress snippet”, but really just the PHP include function, using WordPress get_template_directoryto get the relative path for the file.

Why you should choose a relative path and not an absolute/static path

When you want to include a PHP file (or image file, HTML file, etc.), it is required to specify a specific path that instructs the webserver, on where the webserver should locate the specific file. There are multiple ways to go about this, such as:

  • Using a absolute/static path (not recommended) – specifying the location of a file or directory from the root directory(/).
  • Using a relative path (best practice) – the path related to the present working directly(pwd), starting at your current directory and never starts with a “/”.

The WordPress get_template_directory() function

The build-in WordPress function get_template_directory, retrieves the current theme directory by returning an absolute server path (eg: /home/user/public_html/wp-content/themes/my_theme), and not a URI.

In case you are using a WordPress child theme

In case you are using a child theme, the absolute path to the parent theme directory will be returned – and this will not work. If you are using a child theme, then you would have to use the WordPress function: get_stylesheet_directory() instead, to get the absolute path to the child theme directory.

How to include a PHP file in a WordPress theme

In this PHP include example, we are going to use a relative path, using the WordPress build in function: get_template_directory.

<?php include get_template_directory() . '/inc/yourfile.php'; ?>

How to include a PHP file in a WordPress child theme

In this PHP include example, we are going to use a relative path, using the WordPress build in function: get_stylesheet_directory().

<?php include get_stylesheet_directory() . '/inc/yourfile.php'; ?>

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

This could be the beginning of something great 🙌

"*" indicates required fields

Name*
Hidden
Hidden

By submitting this form: You agree to the processing of the submitted data in accordance with WP Dynamics Privacy Policy.

Contact Information
You can also write us an email or contact us by telephone.
Telephone +45 71 74 71 21

Support For support inquiries, please refer to our support section.

Recent Articles

Faust.js – A Headless Framework for WordPress

Faust.js is a Headless WordPress Framework that provides a set of tools for building front-end applications based on WordPress ...

How to Load (Enqueue) and Handle Assets and Dependencies in WordPress

In WordPress, instead of adding assets such as styles and JavaScript to the header, you can and should use ...

An Introduction to Google Tag Manager Server-side Tagging

WordPress has come a long way since its launch in 2003 - from a blogging tool to a full-fledge ...