WordPress.org

Ready to get started?Download WordPress

Forums

Using PHP includes in templates? (12 posts)

  1. Roy Tanck
    Member
    Posted 9 years ago #

    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?

  2. Joshua Sigar
    Member
    Posted 9 years ago #

    Something like the following will point to mypage.php in your active theme directory.
    <?php include (TEMPLATEPATH . '/mypage.php'); ?>

  3. Roy Tanck
    Member
    Posted 9 years ago #

    Thanks, that helped!

  4. mahalie
    Member
    Posted 8 years ago #

    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/ and my server is dreamhost.com 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.

  5. jalenack
    Member
    Posted 8 years ago #

    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.

  6. mahalie
    Member
    Posted 8 years ago #

    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!

  7. jalenack
    Member
    Posted 8 years ago #

    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.

  8. mahalie
    Member
    Posted 8 years ago #

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

  9. jalenack
    Member
    Posted 8 years ago #

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

  10. mahalie
    Member
    Posted 8 years ago #

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

  11. theblade24
    Member
    Posted 8 years ago #

    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?

  12. MichaelH
    Member
    Posted 8 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic