102 Responses

  1. Ryan
    Ryan Published |

    Excellent as always. Thanks for the clarification.

  2. Ryan
    Ryan Published |

    I often worry that WordPress is overly complicating themes with custom functions for everything like this, when a simple require() or include() would often do the trick just as well.

    I use the functions because they’re there and it’s the standard thing to do. But it does seem counter-intuitive to suggest the exact opposite of what every standard PHP tutorial will say.

    Most of these make complete sense for parent/child theme scenarios, but the vast majority of themes are neither parent or child themes, so the benefits seem fairly minor to me.

  3. Hiranthi
    Hiranthi Published |

    I see you’re not mentioning load_template(), my I ask why not?

  4. Ben Ackles
    Ben Ackles Published |

    I never understood programming until I started reading your blog. You speak with such clarity on a subject many people find confusing. Keep up the good work!

    Can you provide a link to the first edition of “how to better code WordPress themes” series? Thanks!

  5. Dennis
    Dennis Published |

    Thank you, this is really useful- the last days I have been worrying about whether I include the theme files correctly and now I am happy that I have a better understanding about this.
    I hope there will be more articles about “how to better code WordPress themes”, this is really what I need.

    Keep it up 🙂

  6. paul
    paul Published |

    Thanks for sharing your knowledge. It is very valuable information.

  7. GlamourGaby
    GlamourGaby Published |

    If you use this: require_once( TEMPLATEPATH . ‘/shortcodes.php’ );
    Then the variables used in your main file will be passed to the script from the file “shortcodes.php”

    If you use the “get_template_part” function then the variables are not passed on to the file so you would have to create them again in that file.

    Just keep that in mind if you run into any troubles 🙂

    1. Edd Temple-Smithson
      Edd Temple-Smithson Published |

      Just thought I’d draw a bit more attention to this point as its caused me about an hour of head scratching!

      Using get_template_part() should be exactly what I need as Im calling in a part of a template… but for whatever reason calling this loses reference to the variables ive declared just before calling it.

      I think it’s probably because the page template im making is calling in the content of 4 other pages and for each of these pages I am in turn calling in this template part.

      The template part makes use of $page_id, so i imagine when im calling get_template_part() it is using the $page_id of the parent page, and not the value ive assigned to it when looping thrugh the 4 other pages who’s content im pulling in…if that makes sense(!?)

      Anyway, what this guy said, using require() gets me around this problem so many thanks!

      Ive included some code below to hopefully illustrate what i mean, hope this helps someone…

      Also great article 🙂

      1. Edd Temple-Smithson
        Edd Temple-Smithson Published |

        Ok balls to it. Sorry for the moderation nightmare but please do post the original comment minus the code! 🙂

  8. mkjones
    mkjones Published |

    Another great one Justin 🙂 this reminded me of a few tricks I’d forgotten existed!

    Keep it up!

  9. Joe Random
    Joe Random Published |

    This info is a bit over my head but the step-by-step outline that you’ve provided really helps. I appreciate the easy to follow instructions. It makes it a lot easier to wrap my slow brain around this process…HA!

  10. Yudiacro
    Yudiacro Published |

    I use these functions everyday, but not pretty knowing how to use it properly. Thanks for pointing this out.

  11. Doug 'the Hellraiser' Jackson
    Doug 'the Hellraiser' Jackson Published |

    Some great refreshers there and some new info that looks very useful. Well set out and easy to read guidelines. Will be rereading this a few times

  12. Marie
    Marie Published |

    Hi! I’m sorry if I ask a completely stupid question (but I’m totaly new and don’t speak english very good).

    I’m trying to convert a html-site to wordpress, and now I’m stuck (again) on how to load a file. But I’m not sure it is the same thing you’re talking about here…?
    If I have a list of pdf:s in the sidebar and wan’t people to be able to download/open them… is that the same thing as “load a file”? Because I can’t get it to work.
    In my html file it’s like:

    <a href="documents/name.pdf" rel="nofollow">My pdf</a>

    Should I use “locate_template()” in any way??

  13. Marie
    Marie Published |

    The code just disapears 🙂

  14. Kevin Shah
    Kevin Shah Published |

    Sorry but i had a problem doin this

    can some one help me out

  15. Christopher Neetz
    Christopher Neetz Published |

    Wow, this is the best tutorial I have seen on the subject. Excellent work.

    1. Joshua Chase
      Joshua Chase Published |

      Yes, yes.. I agree with Christopher. Amazing tutorial, this is a perma bookmark. Thanks for taking the time to write this up.

  16. grant stringer
    grant stringer Published |

    Thanks for the insight Justin. I have been playing with wordpress for a while but I think I need to go back and take another look.

  17. Jeff
    Jeff Published |

    Hey Justin,

    Your articles are definitely my favorite WordPress tutorials. So thanks for that.

    Regarding this article, I was wondering how using the bloginfo() function would figure into all of this? Are there any circumstances in which that should be used when authoring themes? Thanks.

    1. Travis Watson
      Travis Watson Published |

      I’m going to bump this question! I’ve been using bloginfo and am really just opening my eyes to this other information.

      I’m kinda curious how bad what I’ve been doing is!

  18. Weekly WordPress Review - WPCanada
    Weekly WordPress Review - WPCanada at |
  19. Juan
    Juan Published |

    I’m glad you went through and explained this, I had some quesions about i.t

  20. Danny
    Danny Published |

    I switched from using the wp functions for sidebars and footers a long time ago, in lieu of php includes, when it became clear global variables set in the head or body template files were not being passed.

    For example, if I set a global variable and gave it the value of the post ID, when i would use ‘get_sidebar’ and try to echo the var in the sidebar file, it wouldn’t work.
    While a simple php include would.

    Is this still the case? Is there something I’m missing.

  21. Rev. Voodoo
    Rev. Voodoo Published |

    Phew! Every time you put out an article about WordPress functions, proper use, etc, I find myself with a couple hours work on my hands. Going through my files and cleaning things up, updating, etc. This is the first time I haven’t had to do anything. I got it all right! However the writeup here gave me insight as to what was going on. Even if I get things right, I like to know ‘why’ I got them right. Thanks for the education as always!

  22. Ilan Perez
    Ilan Perez Published |

    Prototype theme Support:
    How do i ammend your theme so that when i add a category all posts within the category are listed in a dropdown

    Thanks in advance


  23. aldewaghry
    aldewaghry Published |

    Thanks for the education as always

  24. Vanessa
    Vanessa Published |

    Thanks for all the steps and everything is on here it was very helpful for me….

  25. Mike
    Mike Published |

    Hey Justin – I’d like to echo the kudos on your site and say ‘thank you’ for taking the time to publish such clear and easily-digestible WP tutorials — for free =) I visit this site frequently, because 1) your writing style is fun to read and 2) it’s nice to see how far you’ve come from writing a book to starting up DevPress. I had one question, though, regarding this post. What is the best/proper way to include custom javascript (not a plugin) into your theme? Typically, I’ll just add it in the header.php file and wrap it in some conditional statement, so that it only loads on the pages where it’s necessary, but I found that while doing so, that I have to create special rules for the WP Minify plugin. Just wanted to know what your thoughts were on this. Thank you!

  26. Jeb
    Jeb Published |

    Thank you for the help. I was thinking about giving up, but you are right, maybe if I just start over with a simpler theme and write the functions myself.

  27. Dan
    Dan Published |

    Thanks for this, I’ve been trying to product my first theme for a few months now and it’s a lot more fiddly than I first anticipated! This post was a big help buddy.

  28. bp24
    bp24 Published |

    great article for a wp newbie


  29. Joel
    Joel Published |

    You can also access the `template_redirect` hook for use with a custom function. It fires just before WP determines what template to use for a given request. For example:

    `add_action( ‘template_redirect’, ‘function_with_template_logic’ );`

    More here.

  30. Nic531
    Nic531 Published |

    I am a new blogger at wordpress and just to drop a comment here for thank you to share this guides with the beginner like me. I really appreciate your hard work!

  31. DeWayne
    DeWayne Published |

    I’ve been using wordpress for a while now and it takes time to find out how to use WP the right way. I really appreciate these WP tutorials, which have answered a lot of my questions. Thanks again for your help.

  32. Ech0
    Ech0 Published |

    I’d almost given up on wordpress after some frustrating experiences until i read your blog. Now i’ve managed to get site up. Many thanks.

  33. Karl
    Karl Published |

    I have recently created a blog, and i guess this guide helped me with most of the things. Thanks buddy. Keep posting similar articles

  34. Powered By TwentyTen | Ryzz.me
    Powered By TwentyTen | Ryzz.me at |
  35. Konstantin
    Konstantin Published |

    Great writeup Justin! Did I mention I love your blog posts? 😉 Keep up the good work 🙂

  36. neel
    neel Published |

    Excellent post. I am a wordpress developer and i doesn’t know most of the functions you described here. your post will help me allot in future projects. thanks for sharing this excellent article.

  37. Joe
    Joe Published |

    I’ve got a question.. it might be simple, but I didn’t find the answer till now. I want to add some php variables to my wordpress blog and I want them to be included in ever page, so I guess there’s a file, like settings, or something like that.. but I can’t find it… where should I add them?

  38. Erik Ford
    Erik Ford Published |

    First off, a sincere thank you for the effort you put in to educate the community. We are making a conscious effort to develop our themes (for clients and commercial releases) with best practices in mind so I am always lurking around your site soaking up much needed knowledge.

    I have a question regarding require_once. We have started to add our custom widgets in its own directory. To keep things tidy, each widget gets its own file. Normally, we would drop require_once in the functions.php file for every widget. Is that the best way to handle this scenario? Also, after reading through your post regarding organizing the functions.php file, should we fire require_once inside of the theme set up function? I am most concerned about the best practice for child theming.

    Thanks again.

  39. nobita
    nobita Published |

    Thank you for having obandes reviewed

    When the WordPress function existed, I thought it right to use get_template_part for the reason for using it.

    By this tutorial, I was able to understand the view.

    I have one question.

    Can I use PHP include function on the template files?

    Reason why

    use require display hello Worldworld

    use get_template_part('example') display hello world

    I’m sorry poor at english

    Thank you

  40. nobita
    nobita Published |


    reason why code is here


  41. nobita
    nobita Published |

    I think the template files must not use PHP include or require.
    The basis I think.
    Template file should not accept the Template part files vars.
    It seems clean.
    PHP include is include all.
    It seems dirty.
    I wanted to know it is right or not.

    Thank you

  42. Those wacky theme paths | CMOS' TechNotes
  43. Lore
    Lore Published |

    Could you expand on how to load my own template file after login, or from a link?

    For example, I wrote a page in my theme called search_advanced.php. I want to load the Advanced Search Form after login.

    Right now after login, it just loads a Page. The Pages in Wordpress don’t allow php, & enabling it via a plugin seems like a bad idea.

    Or can I create a link to my form elsewhere?


  44. Everaldo Matias
    Everaldo Matias Published |

    Very nice the tutorial.

  45. Colin Crawford
    Colin Crawford Published |

    Thanks Justin you have just clarified something with me, I downloaded a very nice free theme and wanted to make it into a Child theme for obvious reasons. In doing so gave Internal Error 500 and the developers said it was straight forward to create a child theme if I followed the WordPress Codex.

    The problem I have found, lies with their Theme Options page and 2 other files were they had used TEMPLATEPATH & STYLESHEETPATH. Not only that your article here says they are both deprecated so why haven’t they sorted this problem out as they are still maintaining the theme even though it is now free.

    They didn’t seem to know why it can’t be made into a child theme, well after spending a day on it and reading this article then it makes sense.

    Well Done for sharing your knowledge, somehow I always end up back on your sites looking for the answers.


  46. cory
    cory Published |

    I have a parent theme whose functions.php file starts:

    if ( get_stylesheet_directory() == get_template_directory() ) {
    	define('OF_FILEPATH', get_template_directory());
    	define('OF_DIRECTORY', get_template_directory_uri());
    } else {
    	define('OF_FILEPATH', get_stylesheet_directory());
    	define('OF_DIRECTORY', get_stylesheet_directory_uri());

    and I cant seem to get my child theme to work. I know it has to do something with this…that and the parent theme has custom function files in a different folder AND the header.php references get_template_directory and get_template_directory_uri all over it. I’ve looked at everything I know how to look at it. Can you point me in the right direction?


  47. WordPress 官方主题审核标准 | OpenShift Wordpress Example Site
  48. Heidi
    Heidi Published |

    I know this post is over 2 years old but I have to say it is VERY relevant and VERY helpful. IMHO the best theme/template article I have read in my quest to fix/customize a parent theme. My problem was I wanted to change A LOT from the parent but the parent had a lot of subfolders and made it difficult and I could not figure a way to 1)check child FIRST 2)if not there get the parents.
    The parent had this in index
    include (TEMPLATEPATH . “/includes/featured.php”)
    Well first it uses TEMPLATEPATH …
    I wanted to edit this in the childs index to use the new /include/ in the child then parent.
    I tried
    include (get_template_directory() . “/includes/featured.php”)
    include (get_stylesheet_directory() . “/includes/featured.php”)
    and those worked but of course one is JUST parent and the other JUST child.
    include (locate_template(“/includes/featured.php”))
    worked perfectly. I hope you approve of its usage in this way 🙂

    Thank You so much for this wonderful article

  49. WordPress: Necessary Reading | Tom J Nowell
  50. Shaun
    Shaun Published |

    Hi Justin,
    What would in your professional opinion be the best/most appropriate way to load custom post types into the functions.php template?

    Previously, I would always just write them directly into my functions.php but lately I’m finding myself wanting to create a more streamlined functions template with less clutter so I was considering placing my CPT’s in their own respective template files and then calling them into the functions.

    Thank you for your time.

  51. Nicole de Kpi
    Nicole de Kpi Published |

    Thank you so much for your post! I’m that newcomer to the Wordpress and trying to find out how to use those templates properly. After reading your post, I got the whole picture and working my way through the jungle.

  53. Miguel
    Miguel Published |

    So I have some files that build up an SEO application (MySQL, PHP, JS, Images, etc… (‘a mini site’)) and they are in a folder called ‘body_call’. Inside there are the body.php, index.php, css/style.css etc. for that application.

    I am working locally on XAMPP and I have buddypress on my site with a theme I bought BuddyBoss.

    I made a template called keywords.php that has the header and the footer of the BuddyBoss theme but the body / content I left blank and I’m trying to call the index.php from the application from inside the template.

    I tried 2 ways: Putting the Application inside the theme (parallel with the template keywords). And it didn’t show the images, and when I clicked in any link it went to localhost/’folder’/keywords/index.php (page not found).

    The second way was putting the application in the root folder, the same problem. Maybe I’m calling the files the wrong way.

    So basically how do I include the index.php folder from the application inside the ‘keywords.php’ template and make the images appear with the forms, the links to work and all the rest?

    I see that in your article you only talk about “usual” files for wordpress. How about if I want to place inside a wordpress template a complete application.

    I’ve been around this problem for 2 days now, so if you can help me that would be great!

    Thanks, and all the best,

  54. Miguel
    Miguel Published |

    Thanks for the quick answer.
    From what you said I built a plugin from the folder with the application. So, the folder is called body_call and inside has the body.php, the index.php (in which I placed the plugin “header copyright”) and other functions like forms and so on. In this folder (body_call) I also have the image folder that has the images, the css folder that has the style.css and the js folder.
    So, I made the function to be called from the new plugin “platform ()” and I called it from a template that I made called keywords (since the platform is about SEO keywords).
    The images do not show! And the style sheet is not accessible, but the content from the body is more or less there (its scramble). I used the path for the images like this: images/image.jpg and the style sheet is called the same way: css/style.css. All this inside the body_call folder (parent folder).
    The plugin shows on (domain / localhost)/keywords but when I click on a link it goes to (domain / localhost)/keywords/index.php and gives a page not found…
    This is my 3rd day working with this basic problem (I can tell its basic, and I figure that the problem is in the calling the files)
    Here is my e-mail:
    I really need this fixed… if you can help that would be super, I would even pay you 20 or 30 Euros for this easy job.
    Let me know,
    Thanks for your reading time,

  55. How to get the path to a file within a WordPress theme | Ben Pearson
  56. BJ
    BJ Published |

    Just Curious Justin,

    Does the locate_template start fishing for templates in the Root of your server or also in your Theme directory like get_template_part() ?

    I really need to get outside my theme directory and include some template from a complete different project on the same server.

  57. Andi
    Andi Published |

    Great writeup Justin! Did I mention I love your blog posts? 😉 Keep up the good work

  58. Mansour
    Mansour Published |


    Thank for this great article. As I understand you say we shouldn’t use

    include(TEMPLATEPATH . "/header.php"); and use get_header();?

    Thanks again


  59. Random Joe
    Random Joe Published |

    I have metro.php file and i use get_templater_part('metro);

    thanks for sharing this, nice post.

    I am new in theme development

  60. Carsten
    Carsten Published |

    Hi Justin

    I can’t seam to get clear on the subject on making a ‘includes’ dir under the child theme, and how to get Wordpress to use those files instead of those from the parent theme ?

    I would also like to get a ‘images’ dir under my child theme 🙂

    I have bin using most of the day reading around, but can’t get it to work, i use a theme called Bold from Elegant Themes.

    All the best


  61. Carsten
    Carsten Published |

    Thanks for your reply Justin.

    So the ‘Includes’ dir is not a standard way of doing things under WP ?

    I just thought so, cause it’s mentioned here in this thread.

    Btw, i have got some of it working with the use of get_stylesheet_directory instead of get_template_directory in the home.php, but some dirs are set/included in the functions.php, and i’m not familiar with how to change dirs with actions in the child themes functions.php.

    All the best


  62. Sofia
    Sofia Published |

    Hey Justin,

    thanks for this awesome help, you should post it on the wordpress forum as well. It took a while to find your article 😉

  63. Tomafrialdi
    Tomafrialdi Published |

    Hey justin,

    Thanks for this, you help me in this post. i have resolve my problem with wp. thanks again.

  64. Afrialdi
    Afrialdi Published |

    I resolve my problem, thanks justin

  65. Afrialdi
    Afrialdi Published |

    I think my problem resolve, so code in wp is very perfect. wp very well run for marketer. thanks justin

  66. Cam
    Cam Published |

    For widgets and/or files that need to be loaded from a subfolder within the theme directory:

    foreach (glob(trailingslashit(get_stylesheet_directory()).”widgets/*.php”) as $file) { require_once $file; }

  67. Victoria
    Victoria Published |

    Thanks for the informational guide, I am getting started with wordpress, Yeah I am late. Anyhow, I am making a theme, in which I might have different skins and hence difference templates for all skins, for example a POST page in skin1 will be different and in SKIN2 will be completely different, so I am using get_template_part() function in single.php like below:

    /* Post Tags Template */

    get_template_part( ‘templates/post_tags’ );

    /* Post Meta Template */

    get_template_part( ‘templates/post_meta’ );

    Is it a good idea to use and load them like this, In fatc with this, it will load different skins and templates based on “Skin” settings. Is that fine to do so OR it will put load on server and not a good idea ?

    thanks once again..

  68. Sepatu Mr Smith
    Sepatu Mr Smith Published |

    Once again, this is great article, Justin. Your blog contains many things new for me. Because i am beginner for using wordpress platform

  69. Brad Dalton
    Brad Dalton Published |

    Thanks for another useful post.

    However, i didn’t see any difference between using either of these in the child theme to load a php file from the child themes root directory.

    require_once( get_stylesheet_directory() . ‘/template-tags.php’);

    require get_stylesheet_directory() . ‘/template-tags.php’;

  70. Richard Mišenčík
    Richard Mišenčík Published |

    Hello, I have a question, lets say Im using a slider on my page. I created a slider.php file with a my_slider() function to keeps things separated and clean. Then I include this file in the functions.php. And when needed I just call the function to display the slider.

    But I would like to know what is the best way to deal with things like this. Or should I just instead of creating a function, do a get_template_part(‘slider’)? So the file doesnt load on every page?

    Or how about this:

    if(!is_admin()) {



    What would be the best way? Is any way better than the other, and are they affecting the load time? Thanks 🙂

  71. RaulM
    RaulM Published |

    finally a real help, you can open up your forum with that many questions hehehe.

    good luck for the future justin

  72. georg
    georg Published |

    problem solved! Muchas gracias

  73. Trevor
    Trevor Published |

    Thank you! This is exactly what I was looking for.

  74. vinayak
    vinayak Published |

    hii.. i am using this function in child theme but it only returning me the file name and path up to that file.. please tell me the cause and solution.

  75. vinayak
    vinayak Published |

    I am little bit confusing about wordpress, i am very new to it but i know php very well even though when it comes to wordpress i feel that i am learning some different web development language. please suggest me some simple books (i also mean in simple English :)) which will help me to learn wordpress from scratch to advanced level. and after that i will start to use yours great book “Professional wordpress plugin development. ” Please, i want your guidance.

  76. Rezaur Chowdhury
    Rezaur Chowdhury Published |

    A good news for windows based server. A new function is added on WordPress 3.9 to clean up the path. It’s wp_normalize_path();


    $bS_incl_path = get_template_directory() . '/inc';
    >$bS_incl_path = wp_normalize_path($bS_incl_path);
    define('THM_INC', $bS_incl_path);
  77. savvas
    savvas Published |

    i know there is a way to run php in a page.

    you place all or most(say except an array of parameters) to a theme/templates folder and you set the template combobox on the editpage GUI to the file with the code

    Is there a better way to do such a thing without templates ie by registering the php file to the page somehow?

    I saw an imlpementation where page loaded code without setting the template.Do you know how this was done?

  78. Abdul Samad
    Abdul Samad Published |

    I have two files deep.js and jq.js how to upload them in header and footer please help..

  79. How to load files within WordPress themes | My Blog
  80. laxman
    laxman Published |

    thank you very much …v

  81. Marcel Hadorn
    Marcel Hadorn Published |

    Great Article that I’ll gladly suggest as reference!

    What are your thoughts about include(locate_template(… as this allows to pass variables from template to loaded template and makes it possible to have a more components like approach with contextual data and without using global variables.

Comments are closed.