{"id":412,"date":"2013-05-07T13:04:13","date_gmt":"2013-05-07T13:04:13","guid":{"rendered":"http:\/\/a1webdesignteam.com\/blog\/?p=412"},"modified":"2013-05-07T13:04:13","modified_gmt":"2013-05-07T13:04:13","slug":"include-require-files-templates-wordpress","status":"publish","type":"post","link":"https:\/\/a1webdesignteam.com\/blog\/include-require-files-templates-wordpress\/","title":{"rendered":"How to Include and Require Files and Templates in WordPress"},"content":{"rendered":"<p>When it comes to PHP, a lot of developers love the language, a lot of the developers hate the language, and a lot of developers generally just use it to get their work done.<\/p>\n<p>For what it\u2019s worth, I\u2019m of the latter camp. I think PHP is fine. Like anything, it\u2019s not without it\u2019s problems, but I enjoy working with it well enough and see it as a way to get work done versus some pie-in-the-sky language for some time of utopia of development.<\/p>\n<p>The thing is, one of the things that developer\u2019s love about PHP \u2013 its features and flexibilities \u2013 are the very things that often trip us up. The list is long, but in the WordPress world, one of the most common points of confusion is the proper way to include external files.<\/p>\n<p>PHP offers four(!) ways to do this and WordPress even offers its own variation thereof.<\/p>\n<p>In this article, we\u2019ll survey the four ways that PHP offers to include files, guidelines for when to use each, and we\u2019ll review WordPress\u2019 features for including files.<\/p>\n<hr \/>\n<h2>Including Files With PHP<\/h2>\n<p>Generally speaking, including files with PHP refers to the action of including another script into the context of the script on which you\u2019re currently working.<\/p>\n<p>You can think of this as importing a file such that when the file is returned from the server, the scripts are combined together in the order that they are included, and then interpreted as a single file.<\/p>\n<p>First, we\u2019ll look at the ways to include files in PHP and the implications of each. At the end of the article, we\u2019ll review when we should be doing this.<\/p>\n<h3><code>include()<\/code><\/h3>\n<p>According to the PHP manual:<\/p>\n<blockquote><p>include() will include and evaluate the specified file. If the file isn\u2019t found, a PHP warning will be thrown.<\/p><\/blockquote>\n<p>Simply put, this means that PHP will look to the file that you\u2019re attempting to include. If it\u2019s found, then it will be added to your script in the exact place that you\u2019ve declared it.<\/p>\n<p>This is important to understand. For example, let\u2019s say you\u2019re writing a set of functions that depend on a prior set of functions. In that case, you\u2019ll need to make sure the other file is included first.<\/p>\n<p>On the other hand, if you\u2019re looking to bring in a set of functions or an external file in the middle of an existing function, then you can include it exactly in the place that you need it.<\/p>\n<p>Secondly, note that if the file is missing, PHP will throw a warning. Depending on the server configuration, you may see this rendered to the browser or you may see this written to a log file (or both).<\/p>\n<p>Nonetheless, warnings are just that \u2013 they aren\u2019t considered fatal and typically don\u2019t stop execution, but they are important to note because it generally implies that part of your work is not being properly loaded and\/or interpreted.<\/p>\n<p>Finally, note that when a file is loaded using <code>include()<\/code> that it will have access to all of the variables previously defined in your existing script.<\/p>\n<p>For example, say that you\u2019re working on a function and halfway through the function, you\u2019re going to include a separate file. That separate file will have access to the variables defined earlier in the function in which it\u2019s included.<\/p>\n<p>Although you may view this as convenient, it makes the external script a bit unclear as it doesn\u2019t necessarily show that it\u2019s depending on variables defined externally. This can be confusing especially when working with a team.<\/p>\n<h3><code>include_once()<\/code><\/h3>\n<p>Taken directly from the PHP manual:<\/p>\n<blockquote><p>include_once() will perform the same behavior as include() but won\u2019t include the file again if it\u2019s already been included.<\/p><\/blockquote>\n<p>Obviously, there\u2019s no need to spend as much time talking about the general points of <code>include_once()<\/code>, but there are some key differentiators between how <code>include_once()<\/code> works and how <code>include()<\/code> works.<\/p>\n<p>First, whereas <code>include_once()<\/code> performs largely the same as <code>include()<\/code>, it will\u00a0<em>not<\/em> allow you to include the script again. This means that if somewhere else in your project, an external file has been included, that\u2019s\u00a0<em>the definitive location<\/em> in which that file has been included.<\/p>\n<p>So what\u2019s the advantage of this? Aside from assuring that there\u2019s only a single place in which a script is included, it also ensures that variables and functions won\u2019t necessarily be redefined. Recall that when using <code>include()<\/code>, scripts have access to functions and variables that are defined above them.<\/p>\n<p>If you opt to define a new set of variables or functions in a file, include it in another script, and\u00a0<em>not<\/em> use <code>include_once()<\/code>, then you run the risk of re-defining functions and variables potentially causing major problems with the execution of your code.<\/p>\n<hr \/>\n<h2>Requiring Files With PHP<\/h2>\n<p>Requiring files is similar to including files in that it\u2019s another way that you can include a script into the script that you\u2019re currently writing, but it carries a set of its own implications around errors and security.<\/p>\n<p>Though you can consider the act of requiring a file in much the same way that you can including a file, it does carry the implication that it\u2019s stronger \u2013 that is, the external file is\u00a0<em>required<\/em> for execution.<\/p>\n<p>As we\u2019ll see, this is exactly this case.<\/p>\n<h3><code>require()<\/code><\/h3>\n<p>Once again, straight from the PHP manual:<\/p>\n<blockquote><p>require() performs the same as include() but will throw a PHP fatal error if the file isn\u2019t found.<\/p><\/blockquote>\n<p>So here\u2019s the thing with <code>require()<\/code>: it will perform the same action as <code>include()<\/code> as far as importing the external script into the context of the one on which you\u2019re working, but if it fails to locate the file, it throws an error and completely halts execution.<\/p>\n<p>This means that your application stops. With <code>include()<\/code>, you\u2019ll get a warning and it will attempt to keep going.<\/p>\n<p>On some level, it may seem like requiring files is\u00a0<em>the<\/em> right way to go. After all, why would you want to risk including something just to have a warning with a potential failure in the application.<\/p>\n<p>But it all comes down to the nature of what you\u2019re working on. Sometimes, simple PHP warnings are okay \u2013 like forgetting to initialize the index of an array \u2013 other times, you need an error.<\/p>\n<p>There aren\u2019t hard and fast rules for when to use this versus includes, but think critically about the nature of what you\u2019re doing and the implications that it carries if it were to fail.<\/p>\n<h3>require_once()<\/h3>\n<p>Lastly, from the PHP manual:<\/p>\n<blockquote><p>require_once() performs the same as require() but will not include the file a second time if it\u2019s already included.<\/p><\/blockquote>\n<p>This is possibly the easiest to understand since we\u2019ve covered the last three functions in relative detail. Simply put, <code>require_once()<\/code> performs the exact same functions as require, though it will not attempt to include a file again if it\u2019s already loaded in your script.<\/p>\n<hr \/>\n<h2>Rules of Thumb<\/h2>\n<p>In addition to critically thinking through which function is best for the nature of your project, here are two additional rules of thumb to consider when working with <code>include()<\/code> and <code>require()<\/code>:<\/p>\n<ul>\n<li><code>require_once()<\/code> is better for larger sites as it does some additional work at the lower level that impacts security and performance<\/li>\n<li><code>include_once()<\/code> is faster and is generally deemed acceptable for smaller sites<\/li>\n<\/ul>\n<p>Easy enough, but what about WordPress helper functions?<\/p>\n<hr \/>\n<h2>Including Files With WordPress<\/h2>\n<p>With all of that said, there\u00a0<em>are<\/em> better ways than using <code>include()<\/code> and <code>require()<\/code> to include templates in your WordPress projects.<\/p>\n<p>Say, for example, that you have several loop files \u2013 one for a post format:<\/p>\n<ul>\n<li>loop-standard.php<\/li>\n<li>loop-image.php<\/li>\n<li>loop-quote.php<\/li>\n<\/ul>\n<p>And you need to include them in <strong>single.php<\/strong> whenever you\u2019re working on a theme\u2019s single post page.<\/p>\n<p>At one point in time, it was acceptable to do something like this:<\/p>\n<pre title=\"\">include_once( 'loop-standard.php' );<\/pre>\n<p>But that\u2019s no longer the best practice.<\/p>\n<h3><code>get_template_part()<\/code><\/h3>\n<p>WordPress now offers a function, <code>get_template_part()<\/code>, that is part of the native API and is used specifically for reusing sections \u2013 or templates \u2013 of code (except for the header, footer, and sidebar) through your theme.<\/p>\n<p>The function accepts two arguments:<\/p>\n<ul>\n<li>The first argument is the slug for the template. In the example above, that would be \u2018<code>loop<\/code>\u2018.<\/li>\n<li>The second argument is the name of the template. In the example above, that would be \u2018<code>standard<\/code>\u2018, \u2018<code>quote<\/code>\u2018, or \u2018<code>image<\/code>\u2018.<\/li>\n<\/ul>\n<p>In sticking with our example above, let\u2019s say that we\u2019re in <a title=\"The Loop\" href=\"http:\/\/codex.wordpress.org\/The_Loop\" target=\"_blank\">The Loop<\/a> and we want to include the quote post format template. In that case, we\u2019d do the following:<\/p>\n<pre title=\"\">if( 'quote' == get_post_format() ) {\r\n\r\n\tget_template_part( 'loop', 'quote' );\r\n\r\n}<\/pre>\n<p>Or, assuming that you\u2019ve named your templates to match the post format types, you can do something even cleaner:<\/p>\n<pre title=\"\">get_template_part( 'loop', get_post_format() );<\/pre>\n<p>Clean, right?<\/p>\n<p>You can actually take this a step farther. Let\u2019s say you\u2019ve abstracted your pagination code out into its own template file called <strong>pagination.php<\/strong>. Using <code>get_template_part()<\/code>, you can include this across your entire site in, say, <strong>footer.php<\/strong> or in <strong>index.php<\/strong>, <strong>single.php<\/strong>, <strong>archives.php<\/strong>, etc. simply by adding:<\/p>\n<pre title=\"\">get_template_part( 'pagination ');<\/pre>\n<p>Much easier, isn\u2019t it?<\/p>\n<hr \/>\n<h2>When Should I Use What?<\/h2>\n<p>So after all of this, we still have yet to actually discuss the guidelines of when to use what. By no means am I an authority on this, but here are the rules of thumb that I follow:<\/p>\n<ol>\n<li>In theme development, I always use <code>get_template_part()<\/code>.<\/li>\n<li>In plugin development, I almost always use <code>include_once()<\/code> and I generally use it once in a function. You can see this in my <a href=\"http:\/\/github.com\/tommcfarlin\/WordPress-Widget-Boilerplate\" target=\"_blank\">boilerplates<\/a>.<\/li>\n<li>If the plugin I\u2019m writing is going to be used on a very large site, then I use <code>require_once()<\/code>.<\/li>\n<\/ol>\n<p>That\u2019s all!<\/p>\n<hr \/>\n<h2>Further Reading<\/h2>\n<p>For more detailed explanations of what was covered in this article, be sure to read the manual pages for each of the functions:<\/p>\n<ul>\n<li><a title=\"Include\" href=\"http:\/\/php.net\/manual\/en\/function.include.php\" target=\"_blank\"><code>include()<\/code><\/a><\/li>\n<li><a title=\"include_once\" href=\"http:\/\/php.net\/manual\/en\/function.include-once.php\" target=\"_blank\"><code>include_once()<\/code><\/a><\/li>\n<li><a title=\"require\" href=\"http:\/\/php.net\/manual\/en\/function.require.php\" target=\"_blank\"><code>require()<\/code><\/a><\/li>\n<li><a title=\"require_once\" href=\"http:\/\/php.net\/manual\/en\/function.require-once.php\" target=\"_blank\"><code>require_once()<\/code><\/a><\/li>\n<li><a title=\"get_template_part\" href=\"http:\/\/codex.wordpress.org\/Function_Reference\/get_template_part\" target=\"_blank\"><code>get_template_part()<\/code><\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>When it comes to PHP, a lot of developers love the language, a lot of the developers hate the language, and a lot of developers generally just use it to get their work done. For what it\u2019s worth, I\u2019m of the latter camp. I think PHP is fine. Like anything, it\u2019s not without it\u2019s problems, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0},"categories":[7,4],"tags":[],"_links":{"self":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/posts\/412"}],"collection":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/comments?post=412"}],"version-history":[{"count":0,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/posts\/412\/revisions"}],"wp:attachment":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/media?parent=412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/categories?post=412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/tags?post=412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}