6

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?

<?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 &#187;', '1 Comment &#187;', '% Comments &#187;'); ?>
  </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'); ?>
  • Take a look at codex.wordpress.org/Integrating_WordPress_with_Your_Website should answer your question. – Mark Davidson Aug 26 '13 at 7:37
  • 1
    You can use it. What's the problem are you facing? – Vinod Dalvi Aug 26 '13 at 7:39
  • hi, i just copied my files inside the wordpress folder and linked them properly.. anyway, whats the answer on this question? I just get a blank white page – Jeremi Liwanag Aug 26 '13 at 14:07
  • Just to be clear, you are trying to include files that are located on an external site? If so, do the current permissions of the php files site allow public read access to these files? – iyrin Feb 13 '14 at 14:08
8

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.

1

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.

  • 1
    Please add a good deal of explanation to your answers to help the user understand your point. – Maruti Mohanty Feb 3 '14 at 11:11
0

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 );
?>
0

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()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.