Include a external PHP file into a WordPress Custom Template

No, you cannot include PHP files in a theme the way you indicated.

To see why, all we have to do is exercise some common sense and basic critical thinking:

  1. You would be doing a remote request to a remote server include cannot do this
  2. If by some miracle it worked, you would have a major security exploit as I could then do something like this: include( 'http://yoursite/wp-config.php' ); echo DB_PASSWORD;. Imagine how easy it would be to hack websites! Whats to stop me including facebook.com/index.php ?
  3. If even then it somehow worked, and it was secure, imagine the slowness! You make a request to the server, and then the server goes and makes another for the header, and another for the sidebar, and another for the footer, all that waiting, who could be bothered?
  4. But finally, even if include could make remote requests. Which it can’t, why would it return PHP code? Won’t it return what the server spits out, aka the finalised html? The server runs any PHP before it gets returned so there’d be no PHP in what your including..

So no, it’s not possible. But a quick copy paste of the code in your question into a PHP script followed by a quick visit would have told you that quicker than anybody here.

Instead, you can only include files from the local file system your server is currently running on. You would never pass in a URL to an include or a require statement.

So if I’m in index.php, and there is a file in the same folder called sidebar.php, I would include it like this:

include('sidebar.php');

include returns a value if this failed. The semantics of the include statement are general basic PHP and are off topic on this Q&A site.

However I kept this question open because there is a WordPress API function you should be using instead that is better than include and require for what you need.

To include a template part in a theme, use:

get_template_part( 'partname' );

This function will also look inside a child or parent theme for the part you specified.

For example, in this code:

get_template_part( 'sidebar', 'first' );

It will look for and include the first it finds of these:

  • child theme sidebar-first.php
  • parent theme sidebar-first.php
  • child theme sidebar.php
  • parent theme sidebar.php

It should be noted, that you should only use get_template_part to include template files. Don’t use it to include files such as libraries, or other PHP files that do not output html.

I suggest you read up on the following subjects before continuing:

  • the include and require functions on PHP.Net
  • parent and child themes
  • the template hierarchy and how WordPress chooses which template to use
  • <?php php ?> <?php tag ?> <?php spam ?> <?php and ?> <?php why ?> <?php its ?> <?php bad ?>
  • get_template_part
  • the WP_HTTP API, for those genuine times you really need to do a remote request to an API. Functions such as wp_remote_get and wp_remote_post will help here.
  • PHP Debugging. Your white pages are PHP Fatal errors, but because you have error logging set to hide it from the front end, it’s being logged to an error log file you’re unaware of. Find your error log, define WP_DEBUG in your wp-config.php and install a plugin such as debug bar or query monitor, these will help you enormously. Make sure you call wp_footer in your templates footer though.

Leave a Comment