{"id":8694,"date":"2020-06-21T15:00:30","date_gmt":"2020-06-21T13:00:30","guid":{"rendered":"https:\/\/qodeinteractive.com\/magazine\/?p=8694"},"modified":"2020-11-13T12:47:35","modified_gmt":"2020-11-13T11:47:35","slug":"add-header-and-footer-code-in-wordpress","status":"publish","type":"post","link":"https:\/\/qodeinteractive.com\/magazine\/add-header-and-footer-code-in-wordpress\/","title":{"rendered":"How to Add Code to Your WordPress Header and Footer"},"content":{"rendered":"<div class=\"wpb-content-wrapper\"><p>[vc_row][vc_column][vc_column_text]Many tools and tracking scripts require you to add certain code to the header and footer areas of your website. This makes it very likely that you will have to edit these elements at some point. Fortunately, there are a couple of straightforward ways you can achieve this.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]In this article, we will show you how you can add header and footer code in WordPress either using plugins or manually. You just need to choose which of the two ways suits you better for modifying these elements.[\/vc_column_text][vc_empty_space height=&#8221;68px&#8221;][\/vc_column][\/vc_row][vc_row][vc_column][vc_column_text]<\/p>\n<h2 class=\"qodef-h4\">How to Add Code to Your WordPress Header and Footer Using a Plugin<\/h2>\n<p>[\/vc_column_text][vc_column_text]For most users, <strong>the easiest way to add code to the header and footer area of a website is with the help of a plugin<\/strong>. The advantage of using a plugin for this purpose is that the <strong>code remains even if you change your theme<\/strong>. Additionally, <strong>a plugin doesn\u2019t require you to delve into the code<\/strong> and structure of WordPress files.<\/p>\n<p>[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]<strong>There are several plugins that you can use for this purpose<\/strong>. In this article, we will utilize the <a href=\"https:\/\/wordpress.org\/plugins\/header-footer\/\" target=\"_blank\" rel=\"noopener noreferrer\">Head, Footer and Post Injections plugin<\/a> which is free. You can install it to be able to follow along with the next steps or choose another one if you prefer.[\/vc_column_text][vc_empty_space height=&#8221;50px&#8221;]<div class=\"qodef-single-image-holder   qodef-has-border \">\n    <div class=\"qodef-si-inner\" >\n                                    <img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"374\" src=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/06\/Head-Footer-and-Post-Injections.jpg\" class=\"attachment-full size-full\" alt=\"Head, Footer and Post Injections plugin\" srcset=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/06\/Head-Footer-and-Post-Injections.jpg 700w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/06\/Head-Footer-and-Post-Injections-300x160.jpg 300w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/06\/Head-Footer-and-Post-Injections-620x331.jpg 620w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/>                        <\/div>\n<\/div>[vc_empty_space height=&#8221;38px&#8221;][vc_column_text]After you install and activate your plugin, <strong>go to Settings &gt; Header and Footer<\/strong> in your admin panel. You will see the plugin interface and from there you need to <strong>select the Head and Footer tab<\/strong>.[\/vc_column_text][vc_empty_space height=&#8221;50px&#8221;]<div class=\"qodef-single-image-holder   qodef-has-border \">\n    <div class=\"qodef-si-inner\" >\n                                    <img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"374\" src=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/06\/1-Add-code-to-your-WordPress-header-and-footer.jpg\" class=\"attachment-full size-full\" alt=\"Add code to your WordPress header and footer\" srcset=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/06\/1-Add-code-to-your-WordPress-header-and-footer.jpg 700w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/06\/1-Add-code-to-your-WordPress-header-and-footer-300x160.jpg 300w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/06\/1-Add-code-to-your-WordPress-header-and-footer-620x331.jpg 620w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/>                        <\/div>\n<\/div>[vc_empty_space height=&#8221;60px&#8221;]<div class=\"qodef-single-image-holder   qodef-has-border \">\n    <div class=\"qodef-si-inner\" >\n                                    <img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"374\" src=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/06\/2-Header-and-footer-using-a-plugin.jpg\" class=\"attachment-full size-full\" alt=\"Header and footer using a plugin\" srcset=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/06\/2-Header-and-footer-using-a-plugin.jpg 700w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/06\/2-Header-and-footer-using-a-plugin-300x160.jpg 300w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/06\/2-Header-and-footer-using-a-plugin-620x331.jpg 620w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/>                        <\/div>\n<\/div>[vc_empty_space height=&#8221;38px&#8221;][vc_column_text]To add code to your website\u2019s header, you need to enter it into one of the fields <strong>under the &lt;HEAD&gt; PAGE SECTION INJECTION section<\/strong>. Depending on which field you use, <strong>the code you enter will appear either in the header of each page or only in the header on the homepage<\/strong>.[\/vc_column_text][vc_empty_space height=&#8221;50px&#8221;]<div class=\"qodef-single-image-holder   qodef-has-border \">\n    <div class=\"qodef-si-inner\" >\n                                    <img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"374\" src=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/06\/3-Page-Section-Injection-Section.jpg\" class=\"attachment-full size-full\" alt=\"Page Section Injection Section\" srcset=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/06\/3-Page-Section-Injection-Section.jpg 700w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/06\/3-Page-Section-Injection-Section-300x160.jpg 300w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/06\/3-Page-Section-Injection-Section-620x331.jpg 620w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/>                        <\/div>\n<\/div>[vc_empty_space height=&#8221;38px&#8221;][vc_column_text]You will most likely be <strong>adding code to the first field so that it\u2019ll be in the header of each page<\/strong>. For example, if you want to add a <a href=\"https:\/\/developers.google.com\/analytics\/devguides\/collection\/analyticsjs\" target=\"_blank\" rel=\"noopener noreferrer\">Google Analytics tag<\/a>, all you have to do is <strong>enter the appropriate code in the <em>ON EVERY PAGE<\/em> field<\/strong> and save the change.[\/vc_column_text][vc_empty_space height=&#8221;50px&#8221;]<div class=\"qodef-single-image-holder   qodef-has-border \">\n    <div class=\"qodef-si-inner\" >\n                                    <img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"374\" src=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/06\/4-Enter-the-appropriate-code.jpg\" class=\"attachment-full size-full\" alt=\"Enter the appropriate code\" srcset=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/06\/4-Enter-the-appropriate-code.jpg 700w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/06\/4-Enter-the-appropriate-code-300x160.jpg 300w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/06\/4-Enter-the-appropriate-code-620x331.jpg 620w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/>                        <\/div>\n<\/div>[vc_empty_space height=&#8221;38px&#8221;][vc_column_text]<strong>To add code to your website\u2019s footer<\/strong>, you need to scroll down to<strong>\u00a0the <em>BEFORE THE &lt;\/BODY&gt; CLOSING TAG (FOOTER)<\/em> section<\/strong>. It also contains two fields, but they are slightly different from the ones we had for the header.[\/vc_column_text][vc_empty_space height=&#8221;50px&#8221;]<div class=\"qodef-single-image-holder   qodef-has-border \">\n    <div class=\"qodef-si-inner\" >\n                                    <img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"374\" src=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/06\/5-Add-code-to-your-footer.jpg\" class=\"attachment-full size-full\" alt=\"Add code to your footer\" srcset=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/06\/5-Add-code-to-your-footer.jpg 700w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/06\/5-Add-code-to-your-footer-300x160.jpg 300w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/06\/5-Add-code-to-your-footer-620x331.jpg 620w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/>                        <\/div>\n<\/div>[vc_empty_space height=&#8221;38px&#8221;][vc_column_text]As you can see in the image above, <strong>the two available fields are <em>Desktop and Mobile<\/em><\/strong>.[\/vc_column_text][vc_empty_space height=&#8221;23px&#8221;]<ul class=\"qodef-unordered-list-item \">\n    <li>\n\t        <div class=\"qodef-ul-title-holder\">\n            <span class=\"qodef-ul-title-content\">Desktop \u2013 using this field will make the code appear in <strong>the footer of both desktop and mobile<\/strong> versions of your website (unless the Mobile option is checked)<\/span>        <\/div>\n            <\/li>\n<\/ul><ul class=\"qodef-unordered-list-item \">\n    <li>\n\t        <div class=\"qodef-ul-title-holder\">\n            <span class=\"qodef-ul-title-content\">Mobile \u2013 by checking this option you can <strong>add different code<\/strong> in this field and it will apply only to the mobile version of your website\n<\/span>        <\/div>\n            <\/li>\n<\/ul>[vc_column_text]If you want your code to be present in the footer, regardless of the device the users have, then you should add the code to the Desktop field and leave the Mobile field unchecked. However, <strong>if you need the code for the mobile version of your website to be different, you can check the Mobile option<\/strong> and enter the appropriate code in each field.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]When you&#8217;re done, <strong>don&#8217;t forget to click the Save button<\/strong> at the bottom of the screen.[\/vc_column_text][\/vc_column][\/vc_row][vc_row][vc_column][vc_empty_space height=&#8221;81px&#8221;][vc_separator color=&#8221;custom&#8221; accent_color=&#8221;#f0f0f0&#8243;][vc_empty_space height=&#8221;50px&#8221;][vc_widget_sidebar sidebar_id=&#8221;top-picks-banner&#8221;][vc_empty_space height=&#8221;28px&#8221;][vc_separator color=&#8221;custom&#8221; accent_color=&#8221;#f0f0f0&#8243;][vc_empty_space height=&#8221;80px&#8221;][\/vc_column][\/vc_row][vc_row][vc_column][vc_column_text]<\/p>\n<h2 class=\"qodef-h4\">How to Manually Add Code to Your WordPress Header and Footer<\/h2>\n<p>[\/vc_column_text][vc_column_text]To add header and footer code in WordPress manually, <strong>you need to have some basic knowledge of <a href=\"https:\/\/www.php.net\/\" target=\"_blank\" rel=\"noopener noreferrer\">PHP<\/a><\/strong>. Otherwise, we suggest you use the plugin method described above. With the manual method, you can directly add code to the header and footer areas of your website. This is done via <strong>the <em>header.php<\/em> and <em>footer.php<\/em> files or via the <em>functions.php<\/em> file<\/strong> of your chosen theme.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]If you\u2019re going to add code to your header and footer manually, <strong>you must use a child theme<\/strong> to do so. That way, <strong>the changes aren\u2019t lost<\/strong> when your WordPress theme is updated.<br \/>\n[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]With all Qode themes, you have <strong>blank child themes that you can download<\/strong> along with the main, parent theme to use for making changes. In case you want to <a href=\"https:\/\/qodeinteractive.com\/magazine\/wordpress-create-child-theme\/\">create a new child theme<\/a>, you can do <strong>it in just a few steps<\/strong>.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]Once you have your child theme ready, you need to <strong>choose whether you\u2019ll enter the code in the header and footer directly<\/strong> via the <em>header.php<\/em> and <em>footer.php<\/em> files <strong>or do it via the <em>functions.php<\/em> file<\/strong> of the child theme. We suggest you use the <em>functions.php<\/em> file of your child theme to add the code. This will let you <strong>keep the complete code in one place<\/strong>. Moreover, you wouldn\u2019t need to modify core WordPress files.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]For starters, we\u2019ll take a look at <strong>examples of code snippets<\/strong> that you can use to add header and footer code in WordPress.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]<strong>To add code to your header<\/strong>, you can use this code snippet example:[\/vc_column_text][vc_empty_space height=&#8221;50px&#8221;][vc_column_text]<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/* Make description for code snippet *\/\r\nfunction your_custom_function_name(){\r\n?&gt;\r\nPASTE HEADER CODE WHICH YOU WANT TO ADD HERE\r\n&lt;?php\r\n};\r\nadd_action('wp_head', 'your_custom_function_name');<\/pre>\n<p>[\/vc_column_text][vc_empty_space height=&#8221;18px&#8221;][vc_column_text]<strong>And to add code to your footer<\/strong>, you can use this code snippet example:[\/vc_column_text][vc_empty_space height=&#8221;50px&#8221;][vc_column_text]<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/* Make description for code snippet *\/\r\nfunction your_custom_function_name(){\r\n?&gt;\r\nPASTE FOOTER CODE WHICH YOU WANT TO ADD HERE\r\n&lt;?php\r\n};\r\nadd_action('wp_footer', 'your_custom_function_name');<\/pre>\n<p>[\/vc_column_text][vc_empty_space height=&#8221;18px&#8221;][vc_column_text]<strong>Please note that these snippets do not represent functional code<\/strong> and that you need to adapt them by adding appropriate information. <strong>Enter a short description of the code in the comment field<\/strong> to help you remember its purpose later. Switch the placeholder for the function name with the <strong>actual name of your custom function<\/strong> in both places. And <strong>add your code in the space marked<\/strong> by the all caps text.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]Once your code snippets are ready, you need to add them to the <em>functions.php<\/em> file of your child theme. <strong>The easiest way to access this file is via the Appearance &gt; Theme Editor option<\/strong> in the admin panel. Open it and then just enter the code snippet that you\u2019ve prepared.[\/vc_column_text][vc_empty_space height=&#8221;50px&#8221;]<div class=\"qodef-single-image-holder   qodef-has-border \">\n    <div class=\"qodef-si-inner\" >\n                                    <img loading=\"lazy\" decoding=\"async\" width=\"710\" height=\"378\" src=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/03\/WordPress-Theme-Editor.jpg\" class=\"attachment-full size-full\" alt=\"WordPress Theme Editor Appearance\" srcset=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/03\/WordPress-Theme-Editor.jpg 710w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/03\/WordPress-Theme-Editor-300x160.jpg 300w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/03\/WordPress-Theme-Editor-620x330.jpg 620w\" sizes=\"auto, (max-width: 710px) 100vw, 710px\" \/>                        <\/div>\n<\/div>[vc_empty_space height=&#8221;60px&#8221;]<div class=\"qodef-single-image-holder   qodef-has-border \">\n    <div class=\"qodef-si-inner\" >\n                                    <img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"374\" src=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/06\/6-Code-snippets.jpg\" class=\"attachment-full size-full\" alt=\"Code snippets\" srcset=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/06\/6-Code-snippets.jpg 700w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/06\/6-Code-snippets-300x160.jpg 300w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2020\/06\/6-Code-snippets-620x331.jpg 620w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/>                        <\/div>\n<\/div>[vc_empty_space height=&#8221;38px&#8221;][vc_column_text]The code we used in this example is the same one we employed for explaining the plugin method\u2014namely, the Google Analytics tag. As you can see from the screenshot above, <strong>we used a customized code snippet for the header<\/strong> to add the code. The same principle applies to adding code to the footer.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]In case the Theme Editor option is inactive when you try to open it, you can also access the functions.php file <a href=\"https:\/\/qodeinteractive.com\/magazine\/how-to-use-ftp\/\">using FTP<\/a>. In that case, after entering the code, don&#8217;t forget to save the changes to the file.[\/vc_column_text][vc_empty_space height=&#8221;68px&#8221;][\/vc_column][\/vc_row][vc_row][vc_column][vc_column_text]<\/p>\n<h2 class=\"qodef-h4\">Final Thoughts<\/h2>\n<p>[\/vc_column_text][vc_column_text]Adding header and footer code in WordPress is becoming an increasingly popular practice. It\u2019s a convenient way to customize your website, implement Google Analytics, or improve performance.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]If you decide to add code to your site, then we suggest you use a plugin as it is the simplest method. Especially if you aren\u2019t entirely confident in trying to add code to the header and footer manually. Plugins are suitable for users of all knowledge levels and provide you with all the necessary functionalities. However, if you choose to use the manual method, it will provide you with more freedom in adding code to the header and footer. But keep in mind it still requires a little more knowledge and is intended for more experienced users.[\/vc_column_text][\/vc_column][\/vc_row]<\/p>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Learn how to add header and footer code in WordPress, so you can customize your site, implement analytics, and improve performance.<\/p>\n","protected":false},"author":10600,"featured_media":8716,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[14,4,13],"class_list":["post-8694","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-core","tag-tips","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/posts\/8694","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/users\/10600"}],"replies":[{"embeddable":true,"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/comments?post=8694"}],"version-history":[{"count":0,"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/posts\/8694\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/media\/8716"}],"wp:attachment":[{"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/media?parent=8694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/categories?post=8694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/tags?post=8694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}