making things easy

Using the WordPress Shortcode API to include an external file in the post content

Knowledge Base on October 14th, 2009 6 Comments

Did you ever feel the need to include some PHP code in the post content of WordPress? The post content can accept JavaScript code but not PHP code. The AmberPanther team came up with a WordPress “hack” to overcome this limitation using the Shortcode API.

In the event that some specific code would be needed for a specific post or page, the old school way would be to go into the template files and populate them with if statements. Using our Shortcode decommissions this necessity and allows you to concentrate your energy where it is really needed, expressing your thoughts.

Our mission is to include some PHP code that will be rendered within the post content. For example, let us assume we want to display the current date and time in the middle of the post content using PHP:

$today = date("D d.m.Y H:i:s T");
echo "The date and time at AmberPanther are $today. ";
echo "These are being echoed by PHP code.";
echo "

"; ?>

The above code will yield this output:

The date and time at AmberPanther are Mon 29.05.2017 01:15:56 EEST. These are being echoed by PHP code.

However it is not possible to just type it in the post content while writing your post. The solution is to create a file that will contain the above code and include it in the post content using the Shortcode API.

To do this the first thing that has to be done is to create a Shortcode function in the functions.php file of your theme that will include external files. The AmberPanther Shortcode include function is of this form:

function include_file($atts) {
	//check the input and override the default filepath NULL
	//if filepath was specified
	extract(shortcode_atts(array('filepath' => 'NULL'), $atts));
	//check if the filepath was specified and if the file exists
	if ($filepath!='NULL' && file_exists(TEMPLATEPATH.$filepath)){
	//turno on output buffering to capture script output
	//include the specified file
	//assign the file output to $content variable and clean buffer
	$content = ob_get_clean();
	//return the $content
	//return is important for the output to appear at the correct position
	//in the content
	return $content;
//register the Shortcode handler
add_shortcode('include', 'include_file');

Having declared the Shortcode function and registered its handler then inclusion of custom PHP code in the post content is as easy as ever. To include any PHP file use the following syntax:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.

Duis libero orci, pretium a, convallis quis, pellentesque a, dolor. 

This will include the contents of MyIncludedFile.php between the two lines (1 and 5).

Note: The path to the root directory is appended to the filepath in the Shortcode function. Hence the filepath should be defined starting from the root directory.

Now the possibilities are endless of what you can do within the post content without the need of any if statements!

Tags: , ,

6 Responses to “Using the WordPress Shortcode API to include an external file in the post content”

  1. Sébastien says:


    I wonder about blog search using include file plugin. Will content be included when the post is saved (ie included content is stored in database), or when the post is rendered by wordpress ?



    • AmberPanther says:


      when using the shortcode API WordPress saves only the shortcode in the database, thus in the case of the WP Include File plugin it will save [include file="My_Include_File.php"] and not the content of My_Include_File.php. The content of My_Include_File.php will be included when the post is rendered.

      Now as far as search:
      The WordPress local search will not hit any keywords in the My_Include_File.php . The remedy for this is to SEO the file name with the keywords that are important to that specific post. Thus WordPress local search will find it and display it.

      The major Search Engines will hit keywords within the My_Include_File.php as they fetch the rendered post. In order to get a feeling of how the search engine bots view a specific post you can use Lynx, a command line based browser. This is as close as you can get to viewing your site like a bot does.

      Now specifically for the Googlebot you can use the Webmaster Tools Labs, Fetch as Googlebot to see exactly what the Googlebot sees. It does see the contents of any included files with WP Include File plugin.

      We hope thess answer your question.

      the AmberPanther team

  2. Sébastien says:

    Thanks for this answer.
    Actualy, the file that I’d like to include contains the whole post content. So the internal search question is really important to this case. As I was looking for a way to include external content (from another administration application), I think I could use the XML-RPC interface to send full posts to my WordPress blog.



  3. mikemick says:

    Thank you sooooo much. I was unable to get my shortcode to work originally. It would always place the included file at the top of the page content. Then I found this post and saw that the answer to my problem was output buffering. Thanks again!

  1. […] WordPress does not allow the inclusion of PHP code within the post content. The AmberPanther team explains clearly how to overcome this deficiency… Read the article here… […]

  2. […] If you want to learn more about the philosophy behind WP Include File you can read this article: Using the WordPress Shortcode API to include an external file in the post content […]

Leave a Reply

We stop spam together!

RSS Feed
on facebook