How to Develop a PHP File Include Plugin for WordPress

By Craig Buckler

WordPress can save developers weeks of effort. It’s difficult to justify writing your own CMS when it offers so many features. That said, there are times when you want to inject your own PHP code; perhaps a custom form or a report generated by another system.

To insert your code on every WordPress page or post, you can simply add a PHP include() function to page.php or single.php accordingly, e.g.


Assuming mycode.php resides in your theme folder (wp-content/themes/theme-name), it will be included and executed at that point.

But what if you need to include a file on a specific post/page or within the middle of a content block? We need a WordPress plugin…

Creating the Plugin

Create a new file named php-include.php in your plugins folder (wp-content/plugins) and add a header so it can be identified by WordPress:

Plugin Name: PHP File Includer
Plugin URI:
Description: Include PHP files using a shortcode
Version: 1.0
Author: Craig Buckler
Author URI:
License: Use this how you like!

This is followed by our primary function, PHP_Include(). It expects an array of parameters although we’re just using one — file. If it’s not passed, file set to ‘default’:

// include PHP file
function PHP_Include($params = array()) {

	    'file' => 'default'
	), $params));
	include(get_theme_root() . '/' . get_template() . "/$file.php");
	return ob_get_clean();

The function assumes you’re requesting a file which resides in the theme folder. The last three lines include the file and return its executed contents using PHP’s output buffering functions.

warning: With great power…

…comes great responsibility. This plugin may be small but it allows anyone to execute arbitrary code. That’s not be a problem if you’re the sole editor of your own blog, but you should be wary of other users.

Optionally, you could modify the include statement’s location or only permit files with a specific name pattern, e.g. “/include/mycode-$file.php”. This should safeguard against users including any PHP file.

Finally, we’ll register our function as a shortcode handler:

// register shortcode
add_shortcode('phpinclude', 'PHP_Include');

Save the file and activate the plugin in your WordPress control panel.

Including a PHP File

The following shortcode can now be added to any page or post:

[phpinclude file='mycode']

Assuming mycode.php exists in your theme’s folder, it will be inserted at that point in the content.

I hope you find it useful.

  • I just paiid $ for $23.87 an iPad 2 32-GB and my girlfriend loves her Panasonic Lumix GF 1 Camera that we got for $ 38.76 there arriving tomorrow by UPS. I will never pay such expensive retail prices in stores again. Especially when I also sold a 40 inch LED TV to my boss for $ 657 which only cost me $ 62.81 to buy. Here is the website we use to get it al from,

  • I just paîd $22.85 for an îPad 2-64GB and my girlfriend loves her Canon EOS Rebel Camera that we got for $38.78 there arriving tomorrow by UPS. I will never pay such expensive retail prices in stores again. Especially when I also sold a 40 inch LED TV to my boss for $625 which only cost me $62.81 to buy. Here is the website we use to get it all from, CentHub.cöm

  • Anonymous

    i usually just put the include inside a conditional tag in my single.php or whatever template, like this…

    if( is_page(3) or is_cat(‘jquery’) ){

    i guess this may be a better way, but it seems more complex, although i guess it may be more useful to abstact the code out to the functions.php file instead of editting the template directly.

  • A word of caution about get_template()…this function will return the parent theme in the case that the active theme is a child of another theme.

    So if your theme is a child to parent:

    the above plugin will look in /themes/parent-theme/$file.php

    See Notes section for more info

    You could do something like this in the child theme’s functions.php file as a way to always reference that theme even if it’s a child:

    if (!defined('THEME_ROOT_URI')) {
        define('THEME_ROOT_URI', get_theme_root_uri() . '/' . basename(dirname(__FILE__)));
  • Actually, my fault…the above code could be useful if you need to generate urls to resources within the child theme, disregard/delete my last post :) Doesn’t apply.

    • Although, the function get_template() will return the parent theme, so getting the right theme could be (from functions.php in child theme):

      if (!defined('THEME_ROOT')) {
          define('THEME_ROOT', get_theme_root() . DIRECTORY_SEPARATOR . basename(dirname(__FILE__)));
  • I am still trying to learn about the web design form and how to link to database using php and mysql

  • Well. I came through delicious as one of the top saved article. I thought what would be here. But after reaching here I understood what functions.php can do. Thanks :)

Get the latest in WordPress, once a week, for free.