Include a external PHP file into a WordPress Custom Template

Is it possible to include a external PHP file into a custom template?

I’m trying to add a blog into my site. I already have the header, footer, and sidebar layout in my site. Can I use those in my custom template?

Read More
<?php
/*
Template Name: Blog
*/
?>
<?php include( PATH . 'http://www.example.com/includes/header.php' );  ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
  <h2><a href="<?php the_permalink() ?>">
    <?php the_title(); ?>
    </a></h2>
  <?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
  <div class="entry">
    <?php the_content(); ?>
  </div>
  <div class="postmetadata">
    <?php the_tags('Tags: ', ', ', '<br />'); ?>
    Posted in
    <?php the_category(', ') ?>
    |
    <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?>
  </div>
</div>
<?php endwhile; ?>
<div class="mypagination">
  <?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
</div>
<?php else : ?>
<h2>Not Found</h2>
<?php endif; ?>
<?php include( PATH . 'http://www.example.com/includes/sidebar.php' );  ?>
<?php include( PATH . 'http://www.example.com/includes/footer.php' );  ?>

I also tried:

<?php include('http://mywebsite.com/includes/header.php'); ?>

Related posts

6 comments

  1. 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.
  2. suppose you have this cenario:

    Your wordpress instalation is hosted on ‘www’ root folder as your site, and your header.php is on ‘www/includes’ folder.

    In you wordpress theme, just add

    <?php
    include('includes/header.php');
    ?>
    

    When WordPress reads theme files, it uses the root path.

  3. You cannot include an external resource on include() functions! Include works by server internal paths. You cannot include an url as you want. It’s a PHP security problem!

    You can use:

    get_file_contents('http://your.url'); 
    

    or use a Curl library.

  4. For Child Theme:

    <?php echo get_stylesheet_directory_uri().'/subfoldername/add.php';?>
    

    You can use

    For Parent Theme: get_template_directory() or get_template_directory_uri()
    For Child Theme: get_stylesheet_directory() or get_stylesheet_directory_uri()
    
  5. When i checked the core files following is the code i found for
    in the file wp/wp-includes/general-template.php for function get_header()

    i found following lines

    $templates[] = 'header.php';
    
    locate_template( $templates, true );
    

    So I am using it in my code (index.php) like this

    <?php get_header();
    
        $myphpfile[] = 'grid.php';
        locate_template( $myphpfile, true );
    ?>
    
  6. Might be poor form to post on such an old question, but I recently had to do something similar and found this result while using a method not shown here. Jotting it down for future explorers ;).

    My use case was:

    • WordPress installed in subdirectory
    • I wanted to include footer/header elements from the parent site, rather than using wordpress for these elements

    Because include() or require() can use absolute server paths (eg: /var/www/vhosts/foo.bar/httpdocs/includes/baz.php), we can use WordPress’ ABSPATH definition to generate this:

    include( dirname( ABSPATH ) . '/includes/header.php' );

    this will return /var/www/vhosts/foo.bar/httpdocs/includes/baz.php.

    Even when WordPress is in a subdirectory (eg: /var/www/vhosts/foo.bar/httpdocs/blog/), dirname( ABSPATH ) gives us the document root regardless.

    There might be other ways to do this, using the $_SERVER['DOCUMENT_ROOT'] superglobal for example, but I didn’t explore that.

Comments are closed.