Title: Using PHP includes in templates?
Last modified: August 18, 2016

---

# Using PHP includes in templates?

 *  [weefselkweekje](https://wordpress.org/support/users/weefselkweekje/)
 * (@weefselkweekje)
 * [21 years, 1 month ago](https://wordpress.org/support/topic/using-php-includes-in-templates/)
 * My old 1.2 site had all sorts of includes. I’m trying to convert the same setup
   to a proper 1.5 theme, and I’m having trouble getting the includes to work. I
   can get the theme directory though bloginfo(“theme_directory”), but that echoes
   it instead ot returning it for use in PHP code. I don’t feel much like hardcoding
   the links, as that would make the theme work only in my setup.
 * Is there any other way to find out the theme dir and use it in PHP include statments?
   Any other solutuions?

Viewing 11 replies - 1 through 11 (of 11 total)

 *  [Joshua Sigar](https://wordpress.org/support/users/alphaoide/)
 * (@alphaoide)
 * [21 years, 1 month ago](https://wordpress.org/support/topic/using-php-includes-in-templates/#post-194970)
 * Something like the following will point to `mypage.php` in your active theme 
   directory.
    `<?php include (TEMPLATEPATH . '/mypage.php'); ?>`
 *  Thread Starter [weefselkweekje](https://wordpress.org/support/users/weefselkweekje/)
 * (@weefselkweekje)
 * [21 years, 1 month ago](https://wordpress.org/support/topic/using-php-includes-in-templates/#post-194973)
 * Thanks, that helped!
 *  [mahalie](https://wordpress.org/support/users/mahalie/)
 * (@mahalie)
 * [20 years, 9 months ago](https://wordpress.org/support/topic/using-php-includes-in-templates/#post-195330)
 * My external (outside of WP but not domain) includes have always worked fine, 
   but after upgrading from 1.5.1.2 to 1.5.1.3 they suddenly didn’t work and then
   to 1.5.2 they still don’t work even though everything else seems to be fine (
   permalinks, plugins, etc all working swimmingly).
 * Here’s how I was referencing them:
    ` <?php include($DOCUMENT_ROOT."/includes/
   top-nav.php"); ?>`
 * I attempted to make the reference absolute but my server config won’t allow URL
   file access. I’d rather solve the problem than use an insecure work around anyway.
   Any ideas?
 * Any all help much appreciated, the specific blog I’m having troubles with is 
   [http://www.bergerpartnership.com/news/news_blog/](http://www.bergerpartnership.com/news/news_blog/)
   and my server is [dreamhost.com](http://www.dreamhost.com/r.cgi?mahalie) where
   I am running PHP as CGI and have ‘extra security’ toggled – whatever that means.
   I suppose if I can’t figure it out I will try running PHP regularly next.
 *  [jalenack](https://wordpress.org/support/users/jalenack/)
 * (@jalenack)
 * [20 years, 9 months ago](https://wordpress.org/support/topic/using-php-includes-in-templates/#post-195331)
 * WordPress creates a handy constant for you: ABSPATH.
 * `include(ABSPATH."/includes/top-nav.php");`
 * Also, it has another constant: TEMPLATEPATH which is the directory of the active
   theme.
 *  [mahalie](https://wordpress.org/support/users/mahalie/)
 * (@mahalie)
 * [20 years, 9 months ago](https://wordpress.org/support/topic/using-php-includes-in-templates/#post-195332)
 * Someone on the Dreamhost forum gave me a workaround (obviously I do not know 
   much about PHP). I replaced:
 * ` <?php include($DOCUMENT_ROOT."/includes/top-nav.php"); ?>`
 * with the literal FILE PATH (not the URI), which in my case is something like:
 * `<?php include("/home/.servername/accountname/
    domainname.com/includes/top-nav.
   php"); ?>
 * I’m a PHP noob and wasn’t sure if things had moved about on my server, so I literally
   copied the file path from the error message that was appearing on my page in 
   lieu of the includes since that’s where it was looking for the files.
 * So this a fix, but doesn’t answer why $DOCUMENT_ROOT doesn’t work anymore, I’m
   still curious about that if anyone has any answers!
 *  [jalenack](https://wordpress.org/support/users/jalenack/)
 * (@jalenack)
 * [20 years, 9 months ago](https://wordpress.org/support/topic/using-php-includes-in-templates/#post-195333)
 * I think this is because Dreamhost disabled fopen wrapers. that basically means
   you can’t `include('http://domain.com/file.php');`
 * As I said, use ABSPATH, so you don’t have to hardcode all that /home/.servername/..
   stuff.
 *  [mahalie](https://wordpress.org/support/users/mahalie/)
 * (@mahalie)
 * [20 years, 9 months ago](https://wordpress.org/support/topic/using-php-includes-in-templates/#post-195334)
 * `include(ABSPATH."/includes/top-nav.php");`
 * didn’t work for me…nor did it work after ommitting that first forward slash, 
   since it’s already included in ABSPATH. ABSPATH seems to point to the directory
   of the WP install, as opposed to the theme folder as TEMPLATEPATH does, and not
   to the absolute path of the site/domain which is what I needed since the includes
   are used site-wide and are stored in directories above the WP install.
 * And just to clarify for others having similar problems, it’s true that URL file-
   access is disabled in the server configuration with Dreamhost, which is good 
   because referencing URL’s introduces a lot of security risk. Imagine being able
   to run a program off of anyone’s server by using `include('http://domain.com/
   file.php');`
 *  [jalenack](https://wordpress.org/support/users/jalenack/)
 * (@jalenack)
 * [20 years, 9 months ago](https://wordpress.org/support/topic/using-php-includes-in-templates/#post-195335)
 * will `include(dirname(ABSPATH)."/includes/top-nav.php")` work? You can wrap dirname()
   s as many times as you need, like dirname(dirname(dirname(ABSPATH))) .
 *  [mahalie](https://wordpress.org/support/users/mahalie/)
 * (@mahalie)
 * [20 years, 9 months ago](https://wordpress.org/support/topic/using-php-includes-in-templates/#post-195336)
 * Another helping hand from the great folks over at the dreamhost forum:
 * > `$DOCUMENT_ROOT` should actually be `$_SERVER['DOCUMENT_ROOT']`. Maybe the 
   > current WP release is turning off register_globals?
 * That worked too, so I’m going to use it since the name of my server could change
   someday! It’s also more transferrable to any other templates on other domains
   than typing the literal file path (and easier for PHP noobs like me to use).
 *  [theblade24](https://wordpress.org/support/users/theblade24/)
 * (@theblade24)
 * [20 years, 5 months ago](https://wordpress.org/support/topic/using-php-includes-in-templates/#post-195370)
 * I can get a php include to work if I put it in index.php, or single.php etc. 
   But if I make a “page” I cannot get it to work. What needs to be done differently
   on a user created page?
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [20 years, 5 months ago](https://wordpress.org/support/topic/using-php-includes-in-templates/#post-195371)
 * theblade25: see this thread
    [http://wordpress.org/support/topic/30533#post-172440](http://wordpress.org/support/topic/30533#post-172440)

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘Using PHP includes in templates?’ is closed to new replies.

## Tags

 * [document_root](https://wordpress.org/support/topic-tag/document_root/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 11 replies
 * 6 participants
 * Last reply from: [MichaelH](https://wordpress.org/support/users/michaelh/)
 * Last activity: [20 years, 5 months ago](https://wordpress.org/support/topic/using-php-includes-in-templates/#post-195371)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
