The correct way to include PHP file in WordPress theme and plugin, step by step, all thing about include PHP file in WordPress.
How to Include PHP File in WordPress
For WordPress theme, we need toÂ get_template_directory() function. For WordPress plugin, we have 3 ways to include PHP file.
Include File in Theme
To include file in WordPress theme, just do like this:
include( get_template_directory() . '/my-file.php' );
Include File in Plugin
To include file in WordPress plugin, you have 3 ways:
// Way Number #1 includeÂ dirname( __FILE__ ). '/my-file.php'; // Way Number #2 include( plugin_dir_path(__FILE__).'/my-file.php' ); // Way Number #3 include 'my-file.php';
Read aboutÂ plugin_dir_path() function.
Include WordPress Loop File
This is function for WordPress theme developer, if you have loop in PHP file, and you want to include your loop file in some file for example “index.php” file, create your loop file, for exampleÂ “my-loop.php”, andÂ use this code inside “index.php” file:
<?php get_header(); ?> <div id="theme-wrap"> <?php get_template_part( 'my', 'loop' ); // Include loop file. ?> </div> <?php get_footer(); ?>
Read aboutÂ get_template_part() function.