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 »', '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'); ?>
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:
include
cannot do thisinclude( 'http://yoursite/wp-config.php' ); echo DB_PASSWORD;
. Imagine how easy it would be to hack websites! Whats to stop me includingfacebook.com/index.php
?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
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:
This function will also look inside a child or parent theme for the part you specified.
For example, in this code:
It will look for and include the first it finds of these:
sidebar-first.php
sidebar-first.php
sidebar.php
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:
<?php php ?> <?php tag ?> <?php spam ?> <?php and ?> <?php why ?> <?php its ?> <?php bad ?>
get_template_part
WP_HTTP
API, for those genuine times you really need to do a remote request to an API. Functions such aswp_remote_get
andwp_remote_post
will help here.WP_DEBUG
in yourwp-config.php
and install a plugin such as debug bar or query monitor, these will help you enormously. Make sure you callwp_footer
in your templates footer though.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
When WordPress reads theme files, it uses the root path.
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:
or use a Curl library.
For Child Theme:
You can use
When i checked the core files following is the code i found for
in the file
wp/wp-includes/general-template.php
for functionget_header()
i found following lines
So I am using it in my code (
index.php
) like thisMight 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:
Because
include()
orrequire()
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.