Use locate_template instead of TEMPLATEPATH to Include Files in a BuddyPress Child Theme

Today I ran into an interesting problem while developing a BuddyPress theme. I was attempting to include a custom sidebar using PHP’s “include” function. The WordPress codex page on “include tags” gives us this:

<?php include( TEMPLATEPATH . ‘/sidebar-index.php’ ); ?>

The problem is that the above code doesn’t always work when you are utilizing a child theme (I am building a child theme based on the default BuddyPress theme). Rather, you should use the “locate_template” hook:

<?php locate_template( array( ‘sidebar-index.php’ ), true ) ?>

What the “locate_template” hook does is searches the child theme’s template folder before searching the parent theme’s template folder.

Pretty nifty, eh?

Posted in ,

8 Comments

  1. alan on March 6, 2010 at 3:57 am

    thanks this works perfect…

    thanks for sharing.this is why I like wordpress ,community is awesome!



  2. Marco on March 8, 2010 at 7:28 am

    Hello, I’m facing some problem with this method, I tried to use a hook in child functions.php so I won’t touch files directly, but that didn’t work. Could you drop me a line via mail so I can show you what I’ve done and maybe show me where’s the error.



  3. Toby on March 8, 2010 at 1:40 pm

    Marco,

    Can you explain a little bit more?

    So far, I have only used the “locate_template” hook from a child theme’s page and post template files, so I can’t vouch for how it might work in the functions.php file.



  4. Marco on March 9, 2010 at 4:43 am

    Hello, here’s what I’ve done in functions.php (shouty is the site theme name):

    /*define constant*/
    define(‘SHOUTY_DIR’, STYLESHEETPATH);

    then:
    /*here we insert, after the regular sidebar, Shouty’s left sidebar*/
    add_action(‘bp_after_sidebar’, ‘shouty_include_sidebar_left’);
    function shouty_include_sidebar_left() {
    require(SHOUTY_DIR.’/sidebar_left.php’);
    }

    But this does not load the sidebar_left.php givin an error of file/folder not found in child theme dir, even though is actually there and I can’t figure why. I use this method with frameworks like Hybrid, Thematic and the new Genesis and it always works well.

    ps. I tried hardcoding but doesn’t work either.



    • Toby on March 15, 2010 at 11:08 pm

      Marco,
      Have you tried using include( TEMPLATEPATH . ‘/sidebar-index.php’ ); rather than define(‘SHOUTY_DIR’, STYLESHEETPATH); ?



  5. Marco on March 16, 2010 at 1:42 am

    Yes I did it at first.
    TEMPLATEPATH leads you into the parent theme, giving the error of file/dir not found. Quite the same error as STYLESHEETPATH, this time from the child theme’s dir.



  6. Toby on March 16, 2010 at 8:17 am

    Marco,
    Will the path work properly if you include your code in the template itself (i.e. index.php rather than in functions.php)?



  7. dreamdrivendesigns on October 12, 2010 at 7:40 pm

    Thanks for the tip on inlcude templatepath not working in BuddyPress, I encounter this problem myself. Was looking for a solution. Thanks



RSS From Toby’s Blog

  • “Now THAT was a good meeting!”
    A single effective meeting can change the course of history for a company. Imagine if you could lead effective meetings EVERY DAY! You can, but it takes curiosity & practice. This article is a good place to start your journey to a more effective you.
  • Never Respond to RFPs & Custom Proposal Requests
    Y'all who work at normal 9-5 jobs don't need to deal with the B.S. that is an RFP process (or rather you get paid to deal with it). But us freelancers gotta deal with a whole big bag of schtuff when it comes to custom proposals. Here's my quick-and-dirty RFP filtering process that I use […]
  • What content should I put on my homepage?
    I see websites as one stop of many on the sales journey, useful to help get customers from Point A to Point B in the sales process (or maybe from Point D to Point E).  If you are wondering about what to put on your homepage, start with these basic sales-related questions: Who, specifically, will…

More posts from themightymo.com

How to Connect a GoDaddy Site to ManageWP

By The Mighty Mo! Design Co. | June 27, 2022

GoDaddy owns ManageWP, and, strangely, they make it very difficult to add GoDaddy-managed WordPress sites to their ManageWP service. Thankfully, there’s a quick workaround: Visit https://yoursite.com/wp-admin/plugins.php?showWorker=1 — This will make the ManageWP “Worker” plugin visible. Copy the connection info from the ManageWP Worker plugin. Add the site per-normal on ManageWP. That’s it! I hope this…

How to check if your current page is the wp-login.php page

By The Mighty Mo! Design Co. | June 7, 2022

I realized this morning that my TMM Maintenance Mode WordPress plugin had a bug that was causing the wp-login.php page to be inaccessible. The solution was to write a simple function that checks whether or not we’re currently on a login page, and then add a call to that function in my code. Here’s the…

woocommerce logo

WooCommerce Product Image Gallery Not Loading with WP Rocket Active

By The Mighty Mo! Design Co. | June 6, 2022

Today I updated a WooCommerce site, and everything worked fine, except for the images on product pages – they were not displaying at all. After a lot of trial & error, I realized that WP Rocket was to blame. I’m not sure exactly what the issue was (though my hunch is that it had to…

RSS From Toby’s Blog

  • “Now THAT was a good meeting!”
    A single effective meeting can change the course of history for a company. Imagine if you could lead effective meetings EVERY DAY! You can, but it takes curiosity & practice. This article is a good place to start your journey to a more effective you.
  • Never Respond to RFPs & Custom Proposal Requests
    Y'all who work at normal 9-5 jobs don't need to deal with the B.S. that is an RFP process (or rather you get paid to deal with it). But us freelancers gotta deal with a whole big bag of schtuff when it comes to custom proposals. Here's my quick-and-dirty RFP filtering process that I use […]
  • What content should I put on my homepage?
    I see websites as one stop of many on the sales journey, useful to help get customers from Point A to Point B in the sales process (or maybe from Point D to Point E).  If you are wondering about what to put on your homepage, start with these basic sales-related questions: Who, specifically, will…