Support » Fixing WordPress » Using PHP includes in templates?

  • Roy Tanck


    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)
  • Something like the following will point to mypage.php in your active theme directory.
    <?php include (TEMPLATEPATH . '/mypage.php'); ?>

    Thread Starter Roy Tanck


    Thanks, that helped!

    My external (outside of WP but not domain) includes have always worked fine, but after upgrading from to 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 and my server is 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.

    WordPress creates a handy constant for you: ABSPATH.


    Also, it has another constant: TEMPLATEPATH which is the directory of the active theme.

    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/"); ?>

    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!

    I think this is because Dreamhost disabled fopen wrapers. that basically means you can’t include('');

    As I said, use ABSPATH, so you don’t have to hardcode all that /home/.servername/.. stuff.


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

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

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

    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?

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Using PHP includes in templates?’ is closed to new replies.