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 . '.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 . '.php' ); ?>
<?php include( PATH . '.php' ); ?>
I also tried:
<?php include('.php'); ?>
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/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/includes/sidebar.php' ); ?>
<?php include( PATH . 'http://www.example/includes/footer.php' ); ?>
I also tried:
<?php include('http://mywebsite/includes/header.php'); ?>
Share
Improve this question
edited Sep 17, 2016 at 4:36
Ethan Rævan
4,0295 gold badges27 silver badges55 bronze badges
asked Aug 26, 2013 at 7:31
Jeremi LiwanagJeremi Liwanag
2631 gold badge8 silver badges18 bronze badges
4
- Take a look at codex.wordpress/Integrating_WordPress_with_Your_Website should answer your question. – Mark Davidson Commented Aug 26, 2013 at 7:37
- 1 You can use it. What's the problem are you facing? – Vinod Dalvi Commented Aug 26, 2013 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 Commented Aug 26, 2013 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 Commented Feb 13, 2014 at 14:08
6 Answers
Reset to default 8No, 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:
- You would be doing a remote request to a remote server
include
cannot do this - 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 includingfacebook/index.php
? - 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?
- 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 aswp_remote_get
andwp_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 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
<?php include('includes/header.php'); ?>
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:
get_file_contents('http://your.url');
or use a Curl library.
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()
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 );
?>
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.