{"id":172206,"date":"2018-05-24T13:00:13","date_gmt":"2018-05-24T13:00:13","guid":{"rendered":"https:\/\/premium.wpmudev.org\/blog\/?p=172206"},"modified":"2018-05-23T20:13:52","modified_gmt":"2018-05-23T20:13:52","slug":"understanding-how-wordpress-outputs-code-from-child-themes","status":"publish","type":"post","link":"https:\/\/wpmudev.com\/blog\/understanding-how-wordpress-outputs-code-from-child-themes\/","title":{"rendered":"Understanding How WordPress Outputs Code From Child Themes"},"content":{"rendered":"<p>If you&#8217;ve ever needed to make tweaks to a third party theme, or you&#8217;re using a theme framework, you&#8217;ll be familiar with child themes.<\/p>\n<p>They&#8217;re a powerful feature of WordPress, giving you the flexibility to make customizations to any parent theme without losing them when the theme is updated. I use them to make edits to third party themes, or with the framework theme I&#8217;ve developed myself.<\/p>\n<p>However, it can sometimes\u00a0be unclear whether WordPress will be using the code from your parent or your child theme. When it comes to template files, stylesheets and functions files, it can be tricky to work out what&#8217;s being used when.<\/p>\n<p>In this post, I&#8217;ll demystify all this. I&#8217;ll look at each type of file and identify when WordPress will use the code from your parent theme and when it&#8217;ll use the code from your child theme.<\/p>\n<h3>Stylesheets<\/h3>\n<p>Both your parent theme and your child theme will have a stylesheet, because no theme will function without a stylesheet.<\/p>\n<p>In your child theme, your stylesheet will contain a line of code that tells WordPress that this is a child theme and which is the parent theme.<\/p>\n<p>Here&#8217;s the theme I use for my website, which is a child theme. It opens with some commented out text that tells WordPress what&#8217;s going on with the theme:<\/p>\n<div class=\"gist\" data-gist=\"1c3bb4d5435887fc5f12ea13ce42f4a4\" data-gist-file=\"child_opening.css\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/1c3bb4d5435887fc5f12ea13ce42f4a4.js?file=child_opening.css\">Loading gist 1c3bb4d5435887fc5f12ea13ce42f4a4<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>Note the line that begins <code>template<\/code>. This tells WordPress that this is a child theme and that the theme in the <em>compassframework<\/em> folder is the parent.<\/p>\n<p>WordPress will use theme template files from both the child theme and the parent theme (more of which shortly). But if you want to pull in styling from your parent theme, you have to enqueue it in your child theme&#8217;s <em>functions.php<\/em> file.<\/p>\n<p>Add the following to the <em>functions.php<\/em> file in your child theme. If your theme doesn&#8217;t have that file, create one, making sure you add an opening <code>&lt;?php<\/code> tag at the beginning of the file (you don&#8217;t need to add a closing tag at the end).<\/p>\n<div class=\"gist\" data-gist=\"1c3bb4d5435887fc5f12ea13ce42f4a4\" data-gist-file=\"enqueue_parent_style.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/1c3bb4d5435887fc5f12ea13ce42f4a4.js?file=enqueue_parent_style.php\">Loading gist 1c3bb4d5435887fc5f12ea13ce42f4a4<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>The code above assumes that the parent theme has only one stylesheet; if yours has multiple stylesheets, then you&#8217;ll need to enqueue each of them.<\/p>\n<p>Once you&#8217;ve done that, the styles from your parent theme will be used on your site. If you&#8217;re also adding styles to your child theme, you&#8217;ll need to enqueue that stylesheet too. The code below ensures that if there&#8217;s duplication, the styles from the child theme will override those from the parent theme:<\/p>\n<div class=\"gist\" data-gist=\"1c3bb4d5435887fc5f12ea13ce42f4a4\" data-gist-file=\"enqueue_parent_child_style.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/1c3bb4d5435887fc5f12ea13ce42f4a4.js?file=enqueue_parent_child_style.php\">Loading gist 1c3bb4d5435887fc5f12ea13ce42f4a4<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>This enqueues the parent theme&#8217;s stylesheet first, followed by the child theme&#8217;s. Because the child theme&#8217;s styles are pulled in last, they will override those from the parent theme if you add styling for an element that already has styling in the parent theme. This also sets the parent theme&#8217;s stylesheet as a dependency for the child theme&#8217;s stylesheet, ensuring the child theme stylesheet can&#8217;t be loaded unless the one from the parent theme already has.<\/p>\n<p><em>Note: you may have seen a line in the child theme&#8217;s stylesheet used for this in the past. This is no longer best practice because it slows down the loading of stylesheets &#8211; you should use the functions file instead.<\/em><\/p>\n<h3>Template Files<\/h3>\n<p>Now let&#8217;s take a look at theme template files. Which WordPress uses to display a page on your site will depend on two things: the <a href=\"https:\/\/wphierarchy.com\" target=\"_blank\">template hierarchy<\/a> and the fact that you&#8217;re using a child theme.<\/p>\n<p>So let&#8217;s say you&#8217;re viewing the &#8216;WordPress&#8217; category archive page in your site. WordPress will use the template hierarchy to find the most relevant file:<\/p>\n<ol>\n<li>A category archive template file for that specific category, using the slug: <em>category-wordpress.php<\/em><\/li>\n<li>A category archive template file for that specific category, using the ID: <em>category-23.php<\/em><\/li>\n<li>A general category archive file: <em>category.php<\/em><\/li>\n<li>A general archive file: <em>archive.php<\/em><\/li>\n<li>The catch-all <em>index.php<\/em><\/li>\n<\/ol>\n<p>WordPress will look for these in both your parent and child theme. When it comes across the first file in the hierarchy it will use it, whether it&#8217;s in the parent theme or the child theme.<\/p>\n<p>There&#8217;s one exception: when the most relevant template file has a version in both the parent and child themes, it will use the file from the child theme, and ignore the one from the parent theme. This is one of the most common uses for child themes.<\/p>\n<p>Here are a few examples, using the category example above:<\/p>\n<ul>\n<li>If your child theme has <em>archive.php<\/em> and <em>index.php<\/em> and your parent theme has <em>category.php<\/em> and <em>index.php<\/em>, then WordPress will use <em>category.php<\/em> from the parent theme because it&#8217;s highest in the hierarchy.<\/li>\n<li>If your child theme has <em>archive.php<\/em> and <em>index.php<\/em> and your parent theme has <em>archive.php<\/em> and <em>index.php<\/em>, it will use the <em>archive.php<\/em> file from your child theme.<\/li>\n<li>If your child theme has <em>category.php<\/em> and <em>index.php<\/em> and your parent theme has <em>archive.php<\/em> and <em>index.php<\/em>, it will use the <em>category.php<\/em> file from the child theme, as this is highest in the hierarchy.<\/li>\n<\/ul>\n<p>So if you want to override a template file in your parent theme, create a duplicate of that file in the child theme with the code you want.<\/p>\n<h3>Functions<\/h3>\n<p>The final kind of file affected by child and parent themes is the theme functions file. This also applies to functions you add to your themes via include files.<\/p>\n<p>The functions from your child theme will load before those from your parent theme, assuming they&#8217;re attached to the same hook. This is the opposite from stylesheets and template files and means that if you&#8217;re not careful, the functions in your parent theme could override those in your child theme.<\/p>\n<p>It&#8217;s important to note that writing a function in your child theme with the same name as one in your parent theme may cause an error. WordPress doesn&#8217;t like it when two functions have the same name. The exception to this is when you use a function in your child theme to override a pluggable function in your parent theme.<\/p>\n<h4>Pluggable Functions<\/h4>\n<p>You can recognize a pluggable function because it will be wrapped in a conditional check for another function with the same name. In other words, the parent theme is checking whether a function with the same name has already been fired via the child theme. If so, it doesn&#8217;t run the function in the parent theme. So if your parent theme has pluggable functions, you can easily override these in your child theme.<\/p>\n<p>Here&#8217;s an example of a pluggable function from my parent theme, compass framework:<\/p>\n<div class=\"gist\" data-gist=\"1c3bb4d5435887fc5f12ea13ce42f4a4\" data-gist-file=\"compass_posted_on_pluggable.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/1c3bb4d5435887fc5f12ea13ce42f4a4.js?file=compass_posted_on_pluggable.php\">Loading gist 1c3bb4d5435887fc5f12ea13ce42f4a4<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>In a child theme I can write another function with the same name which will override it:<\/p>\n<div class=\"gist\" data-gist=\"1c3bb4d5435887fc5f12ea13ce42f4a4\" data-gist-file=\"compass_posted_on_child.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/1c3bb4d5435887fc5f12ea13ce42f4a4.js?file=compass_posted_on_child.php\">Loading gist 1c3bb4d5435887fc5f12ea13ce42f4a4<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>This will be run instead of the function from the parent theme, because of that <code>if<\/code> check. You don&#8217;t need to use <code>add_action()<\/code> in your child theme to fire the function, as the <code>add_action()<\/code>\u00a0function in the parent theme will do that for the one from your child theme instead. This is because it&#8217;s outside that conditional check.<\/p>\n<h4>Using Priority to Run Parent Theme Functions First<\/h4>\n<p>When I say that the functions from your child theme will run before those from your parent theme (when attached to the same hook), I&#8217;m assuming that they don&#8217;t have priority assigned to them.<\/p>\n<p>Priority is the third parameter of the <code>add_action()<\/code> and <code>add_filter()<\/code> functions. The higher the number you use for this, the later the function will fire. So if a hook has four functions attached to it, with priority <code>5<\/code>, <code>10<\/code>, <code>20<\/code> and <code>30<\/code>, then they will fire in ascending numerical order. It doesn&#8217;t matter what numbers you use; I tend to use round numbers so there&#8217;s a gap in case I need to insert another function at a later date.<\/p>\n<p>If you don&#8217;t apply priority to a function, WordPress gives it the default priority of <code>10<\/code>. So if there&#8217;s a function in your parent theme with no priority attached, and you want to run another function on the same hook from your child theme, and have it fire after the one from the parent theme, just give it a priority higher than <code>10<\/code>.<\/p>\n<p>Let&#8217;s take the pluggable function I used before, which has no priority parameter for the <code>add_action()<\/code> function. If you want to run some other code immediately after it from your child theme, use priority, like this:<\/p>\n<div class=\"gist\" data-gist=\"1c3bb4d5435887fc5f12ea13ce42f4a4\" data-gist-file=\"compasschild_postmeta.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/1c3bb4d5435887fc5f12ea13ce42f4a4.js?file=compasschild_postmeta.php\">Loading gist 1c3bb4d5435887fc5f12ea13ce42f4a4<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>This will add another section with the name of the post author and a link to their archive page, after the date.<\/p>\n<h4>Unhooking Functions<\/h4>\n<p>If a function in your parent theme isn&#8217;t pluggable, but you want to write a function in your child theme to override it, you can still do so. You&#8217;ll need to unhook the function from the parent theme from the hook used to fire it, which means it then won&#8217;t fire. You then write a function with a different name in your child theme to override it, and hook that to the same hook.<\/p>\n<p>Let&#8217;s imagine my pluggable\u00a0<code>compass_posted_on()<\/code> function in the example above isn&#8217;t pluggable. In the parent theme, it looks like this:<\/p>\n<div class=\"gist\" data-gist=\"1c3bb4d5435887fc5f12ea13ce42f4a4\" data-gist-file=\"compass_posted_on_not_pluggable.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/1c3bb4d5435887fc5f12ea13ce42f4a4.js?file=compass_posted_on_not_pluggable.php\">Loading gist 1c3bb4d5435887fc5f12ea13ce42f4a4<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>To override it, first you unhook it using the <code>remove_action()<\/code> function, which you hook to the <code>wp_head<\/code> hook:<\/p>\n<div class=\"gist\" data-gist=\"1c3bb4d5435887fc5f12ea13ce42f4a4\" data-gist-file=\"remove_action.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/1c3bb4d5435887fc5f12ea13ce42f4a4.js?file=remove_action.php\">Loading gist 1c3bb4d5435887fc5f12ea13ce42f4a4<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>You then code a new function into your child theme with a different name, and hook it to the same hook:<\/p>\n<div class=\"gist\" data-gist=\"1c3bb4d5435887fc5f12ea13ce42f4a4\" data-gist-file=\"compasschild_posted_on.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/1c3bb4d5435887fc5f12ea13ce42f4a4.js?file=compasschild_posted_on.php\">Loading gist 1c3bb4d5435887fc5f12ea13ce42f4a4<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<h3>Understanding Parent and Child Themes Will Help You Make Better Use of Them<\/h3>\n<p>The guide above will help you to better understand what WordPress will do with the code from both your parent and child theme when you&#8217;re using child themes. It will help you to troubleshoot situations when the code you&#8217;re expecting isn&#8217;t being used. It will also help you to write better and more efficient code in your child themes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;ve ever needed to make tweaks to a third party theme, or you&#8217;re using a theme framework, you&#8217;ll be familiar with child themes. They&#8217;re a powerful feature of WordPress, giving you the flexibility to make customizations to any parent theme without losing them when the theme is updated. I use them to make edits [&hellip;]<\/p>\n","protected":false},"author":347011,"featured_media":161692,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"blog_reading_time":"","wds_primary_category":0,"wds_primary_tutorials_categories":0,"footnotes":""},"categories":[263],"tags":[10039,684,10866],"tutorials_categories":[],"class_list":["post-172206","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-wordpress-themes","tag-child-themes","tag-free-tutorials"],"_links":{"self":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/172206","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/users\/347011"}],"replies":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/comments?post=172206"}],"version-history":[{"count":10,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/172206\/revisions"}],"predecessor-version":[{"id":172449,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/172206\/revisions\/172449"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media\/161692"}],"wp:attachment":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media?parent=172206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/categories?post=172206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tags?post=172206"},{"taxonomy":"tutorials_categories","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tutorials_categories?post=172206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}