{"id":167141,"date":"2017-08-23T13:00:02","date_gmt":"2017-08-23T13:00:02","guid":{"rendered":"https:\/\/premium.wpmudev.org\/blog\/?p=167141"},"modified":"2022-03-02T04:48:40","modified_gmt":"2022-03-02T04:48:40","slug":"widgetize-page-post-template-wordpress","status":"publish","type":"post","link":"https:\/\/wpmudev.com\/blog\/widgetize-page-post-template-wordpress\/","title":{"rendered":"Adding a Custom Widget to Any Page, Post or Template in WordPress"},"content":{"rendered":"<p>Widgets make WordPress really flexible. By adding widget areas to your themes, you let users insert their own custom content without having to write a line of code.<\/p>\n<p>They&#8217;re most common in sidebars and footers, where they&#8217;re often used to hold widgets such as the search widget, latest posts widget, or maybe a custom menu. But if you add them to your page and post templates you make them even more powerful.<\/p>\n<p>You can add widget areas to any template file you like. I like to add plenty of them to my themes, in places such as the header, before and after the main navigation, and before and after the content. You can either add them to a template part (such as the header) that&#8217;s being used by every page in your site, or you can be more specific and just add them to template files for a given content type.<\/p>\n<p>In this post, I&#8217;m going to show you how to do just that. I&#8217;ll create a child theme of the default Twenty Seventeen theme and then within that child theme, I&#8217;ll create some new template files: a custom page template file, a category archive file and a single post template file.<\/p>\n<p>In each of these I&#8217;m going to add a widget area specific to that content type, so users will only see it when they&#8217;re viewing a page using the page template, a category archive or a single post, respectively.<\/p>\n<p><em>Note: I&#8217;ve added the code for this post to <a href=\"https:\/\/github.com\/rachelmccollin\/wpmu-template-widgets\" target=\"_blank\">GitHub<\/a> so you can check it if you&#8217;re working along\u00a0with this tutorial.<\/em><\/p>\n<p>Continue reading, or jump ahead using these links:<\/p>\n<ul>\n<li><a href=\"#child-theme\">Setting up the Child Theme<\/a><\/li>\n<li><a href=\"#registering-the-widget-areas\">Registering the Widget Areas<\/a><\/li>\n<\/ul>\n<h2 id=\"child-theme\">Setting up the Child Theme<\/h2>\n<p>I&#8217;m going to be using a child of the twenty seventeen theme: if you&#8217;re working with your own theme you can edit that directly instead of creating a child theme. But if you&#8217;re using a third party theme, you must create a child theme. This is because when you update the theme, any changes you make to it will be lost.<\/p>\n<p>So, in your <em>wp-content\/themes<\/em> folder, create a new folder with the name of your theme. I&#8217;m calling mine <em>wpmu-template-widgets<\/em>. Within that, add a <em>style.css<\/em> file and add this to it:<\/p>\n<p><b>\u00a0<\/b><span style=\"font-weight: 400;\"><div class=\"gist\" data-gist=\"4cd6c3f9786c5acfa0c2c411f46c19d7\" data-gist-file=\"style css\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/4cd6c3f9786c5acfa0c2c411f46c19d7.js?file=style+css\">Loading gist 4cd6c3f9786c5acfa0c2c411f46c19d7<\/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><\/span><\/p>\n<p>Remember, if you&#8217;re working with your own theme you can skip this step. And if you&#8217;re using a different parent theme, you&#8217;ll have to edit the stylesheet to reflect that. If you&#8217;re not sure, check out our post on <a href=\"https:\/\/wpmudev.com\/blog\/how-to-create-wordpress-child-theme\/\" target=\"_blank\">creating child themes<\/a>.<\/p>\n<h2 id=\"registering-the-widget-areas\">Registering the Widget Areas<\/h2>\n<p>Before we can add the widget areas to our template files, we need to register them in the theme&#8217;s functions file. Open your functions file if your theme already has one, or if it&#8217;s a new child theme, create a file called <em>functions.php<\/em>.<\/p>\n<p>Let&#8217;s start by adding the empty function to register the widgets, and hooking it to the <code>widgets_init<\/code> hook. Add this to your functions file:<\/p>\n<p><b>\u00a0<\/b><span style=\"font-weight: 400;\"><div class=\"gist\" data-gist=\"98316cf5c79d638375f337b9dede497b\" data-gist-file=\"wpmu widget\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/98316cf5c79d638375f337b9dede497b.js?file=wpmu+widget\">Loading gist 98316cf5c79d638375f337b9dede497b<\/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><\/span><\/p>\n<p>Within that function, we&#8217;re going to register four widget areas: one each of the single post and category archive templates, and two for the custom page template.<\/p>\n<p>Start by registering the first widget:<\/p>\n<p><b>\u00a0<\/b><span style=\"font-weight: 400;\"><div class=\"gist\" data-gist=\"06d367e3a5ac0de27c8a0077b2f09b24\" data-gist-file=\"register single widget\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/06d367e3a5ac0de27c8a0077b2f09b24.js?file=register+single+widget\">Loading gist 06d367e3a5ac0de27c8a0077b2f09b24<\/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><\/span><\/p>\n<p>This uses the <a href=\"https:\/\/codex.wordpress.org\/Function_Reference\/register_sidebar\" target=\"_blank\"><code>register_sidebar()<\/code><\/a> function, with an array of parameters as follows:<\/p>\n<ul>\n<li>The name of the widget area, which will appear in the Widgets admin screen and the Customizer. I&#8217;ve made this translatable.<\/li>\n<li>A unique ID for the widget area.<\/li>\n<li>Markup for before the widget, which uses placeholders for internationalization.<\/li>\n<li>Markup for after the widget.<\/li>\n<li>Markup for before and after the widget title, which is enclosed in a <code>h3<\/code> element.<\/li>\n<\/ul>\n<p>So that&#8217;s the first one. Now, still inside your function, add the other three:<\/p>\n<p><span style=\"font-weight: 400;\"><div class=\"gist\" data-gist=\"49447b9b17d9ea9cb6983fab767a2819\" data-gist-file=\"register 3 widgets\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/49447b9b17d9ea9cb6983fab767a2819.js?file=register+3+widgets\">Loading gist 49447b9b17d9ea9cb6983fab767a2819<\/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><\/span><\/p>\n<p>So we now have four widgets:<\/p>\n<ul>\n<li>One after the content on single posts.<\/li>\n<li>One before the content on a category archive.<\/li>\n<li>One before and one after the content on our widgetized page template.<\/li>\n<\/ul>\n<p>If you want to move your widget areas (e.g. putting the category archive one after the content or the single post one before the content), you should rename them appropriately. This is so that you know what&#8217;s going on when you come to edit your code in future, and your users know where the widget areas will be output when they add widgets to them via the Widgets admin screen. Remember that if you do alter this, you&#8217;ll need to place the code to output the widgets in the correct place in your template files in the next step, which might be different from where I&#8217;m putting mine.<\/p>\n<p>Now if you open your Widgets admin page, you&#8217;ll see your widget areas, ready for widgets:<\/p>\n<div  class=\"wpdui-pic-large   \" >\n<figure class=\"wp-caption alignnone\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"attachment-1050x1050 size-1050x1050\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2017\/08\/widgets-in-admin.png\" alt=\"Newly registered widget areas in the Widgets admin screen\" width=\"1050\" height=\"360\" \/><figcaption class=\"wp-caption-text\">Newly registered widget areas in the Widgets admin screen.<\/figcaption><\/figure>\n<\/div>\n<p>But if you add widgets to your new widget areas, they won&#8217;t show up in your site&#8217;s front end. They still need to be coded into the template files.<\/p>\n<h3>Adding Your Widget Areas to Template Files<\/h3>\n<p>Now we add the code to output the widget areas. If you&#8217;re working with your own theme you may just need to edit your existing theme template files. If you&#8217;re working with a child theme (or your theme doesn&#8217;t already have the relevant template files), you&#8217;ll need to create new files.<\/p>\n<p>Let&#8217;s start with the widgetized page template file, as your theme won&#8217;t have that either way.<\/p>\n<p>In your theme folder, create a new file. I&#8217;m calling mine <em>page_widgetized.php<\/em>. Open that file and add this to it:<\/p>\n<p><span style=\"font-weight: 400;\"><div class=\"gist\" data-gist=\"816979b910a010a367dc5f280ab3eb0d\" data-gist-file=\"page template opener\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/816979b910a010a367dc5f280ab3eb0d.js?file=page+template+opener\">Loading gist 816979b910a010a367dc5f280ab3eb0d<\/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><\/span><\/p>\n<p>This tells WordPress that it&#8217;s a custom page template. If you want to know more about them, check out our <a href=\"https:\/\/wpmudev.com\/blog\/creating-custom-page-templates-in-wordpress\/\" target=\"_blank\">detailed guide<\/a>.<\/p>\n<p><em>Note: Don&#8217;t use <\/em>page-widgetized<em> as your filename. This is because <\/em><a href=\"https:\/\/developer.wordpress.org\/themes\/basics\/template-files\/#common-wordpress-template-files\" target=\"_blank\">page- <em>is a reserved suffix in WordPress<\/em><\/a><em>. Use an underscore instead of a dash, or just avoid using <\/em>page<em> at the beginning altogether if you prefer.<\/em><\/p>\n<p>Now below that commented out text, copy everything from your theme&#8217;s (or the parent theme&#8217;s)\u00a0<em>page.php<\/em> file. An alternative is to make a copy of the file and add the commented out text, it&#8217;s up to you.<\/p>\n<p>You&#8217;ll now be able to select this template when editing a page in your site:<\/p>\n<div  class=\"wpdui-pic-regular  \">\n<figure class=\"wp-caption alignnone\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"attachment-600x600 size-600x600\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2017\/08\/page-template-selector.png\" alt=\"page template selector in the page editing screen\" width=\"600\" height=\"294\" \/><figcaption class=\"wp-caption-text\">The page template selector on the WordPress page editing screen.<\/figcaption><\/figure>\n<\/div>\n<p>Select it for one of your pages so you can try out the widgets. Here&#8217;s my page:<\/p>\n<div  class=\"wpdui-pic-regular  \">\n<figure class=\"wp-caption alignnone\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"attachment-600x600 size-600x600\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2017\/08\/widgetized-page-with-no-widgets-addded.png\" alt=\"page with no widgets displaying yet\" width=\"600\" height=\"436\" \/><figcaption class=\"wp-caption-text\">Page with no widgets displaying yet.<\/figcaption><\/figure>\n<\/div>\n<p>It doesn&#8217;t look very exciting right now as I haven&#8217;t added the widget areas to the template file. Let&#8217;s do that.<\/p>\n<p>In your template file, add this code immediately above the page content, for the above-content widget area:<\/p>\n<p><span style=\"font-weight: 400;\"><div class=\"gist\" data-gist=\"99ceb8bb46e5ab0f2a770358b23656db\" data-gist-file=\"widgetized page template\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/99ceb8bb46e5ab0f2a770358b23656db.js?file=widgetized+page+template\">Loading gist 99ceb8bb46e5ab0f2a770358b23656db<\/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><\/span><\/p>\n<p>This checks if the widget area has been populated, using the unique ID that you created for the widget area when you registered it in your functions file. If it has been populated, it outputs the contents of the widget area.<\/p>\n<p>Now add the widget area below the content. After your content (and inside any enclosing element), add this:<\/p>\n<p><span style=\"font-weight: 400;\"><div class=\"gist\" data-gist=\"1164fca30d8c4274bcb88e56229af995\" data-gist-file=\"widgetized page template\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/1164fca30d8c4274bcb88e56229af995.js?file=widgetized+page+template\">Loading gist 1164fca30d8c4274bcb88e56229af995<\/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><\/span><\/p>\n<p>Save your page template file.<\/p>\n<p>Here&#8217;s the full code of my page template file, so you can see where the widget areas before and after the loop:<\/p>\n<p><span style=\"font-weight: 400;\"><div class=\"gist\" data-gist=\"6745d7914124048fa0065cf65845c63d\" data-gist-file=\"widgetized page template\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/6745d7914124048fa0065cf65845c63d.js?file=widgetized+page+template\">Loading gist 6745d7914124048fa0065cf65845c63d<\/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><\/span><\/p>\n<p>Now to test it out. I&#8217;ve added a widget to each of the new widget areas:<\/p>\n<div  class=\"wpdui-pic-regular  \">\n<figure class=\"wp-caption alignnone\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"attachment-600x600 size-600x600\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2017\/08\/widgets-added-in-admin.png\" alt=\"Widgets admin screens with a widget added to two new widget areas\" width=\"600\" height=\"459\" \/><figcaption class=\"wp-caption-text\">The widgets admin screens with a widget added to two new widget areas.<\/figcaption><\/figure>\n<\/div>\n<p>And here&#8217;s how they look on my page:<\/p>\n<div  class=\"wpdui-pic-regular  \">\n<figure class=\"wp-caption alignnone\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"attachment-600x600 size-600x600\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2017\/08\/widgetized-page-template-front-end.png\" alt=\"A page with widgets before and after the content\" width=\"600\" height=\"459\" \/><figcaption class=\"wp-caption-text\">A page with widgets before and after the content.<\/figcaption><\/figure>\n<\/div>\n<p>So that&#8217;s the custom page template set up. Now you need to edit the other two page template files (or create them if necessary). Follow these steps:<\/p>\n<ul>\n<li>If you&#8217;re working with a child theme, create a file called <em>category.php<\/em>. Copy the contents of the <em>category.php<\/em> file from your parent theme (if it has one) or the <em>archive.php<\/em> file (or <em>index.php<\/em> if it doesn&#8217;t have that).<\/li>\n<li>If you&#8217;re not working with a child theme, but your theme doesn&#8217;t have a\u00a0<em>category.php<\/em> file, make one by copying the <em>archive.php<\/em> file (or if you don&#8217;t have one of those, the <em>index.php<\/em> file).<\/li>\n<li>Add the widget area above the content in your category archive file &#8211; again, immediately before the loop.<\/li>\n<\/ul>\n<p>Here&#8217;s the code for the widget area:<\/p>\n<p><span style=\"font-weight: 400;\"><div class=\"gist\" data-gist=\"aba83b8ea27b8496f329d2ed22aca0fe\" data-gist-file=\"category widget area\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/aba83b8ea27b8496f329d2ed22aca0fe.js?file=category+widget+area\">Loading gist aba83b8ea27b8496f329d2ed22aca0fe<\/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><\/span><\/p>\n<p>Now repeat this for the single template:<\/p>\n<ul>\n<li>Create a <em>single-post.php<\/em> file if your theme doesn&#8217;t have one (or you&#8217;re using a child theme). Copy the contents from your parent theme&#8217;s <em>single-post.php<\/em> file into it, or from the next template file up in the <a href=\"https:\/\/wpmudev.com\/blog\/guide-wordpress-template-hierarchy\/\" target=\"_blank\">template hierarchy<\/a> (<em>single.php<\/em>,\u00a0<em>singular.php<\/em> or <em>index.php<\/em>).<\/li>\n<li>Again, add the widget area &#8211; this time after the content.<\/li>\n<\/ul>\n<p>Here&#8217;s the code:<\/p>\n<p><span style=\"font-weight: 400;\"><div class=\"gist\" data-gist=\"badde16064826f7ad9c2abff7a641f68\" data-gist-file=\"single widget area\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/badde16064826f7ad9c2abff7a641f68.js?file=single+widget+area\">Loading gist badde16064826f7ad9c2abff7a641f68<\/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><\/span><\/p>\n<p>Now if you add widgets, they&#8217;ll show up on your site. Here&#8217;s mine showing a list of recent posts after a single post:<\/p>\n<div  class=\"wpdui-pic-regular  \"> <img loading=\"lazy\" decoding=\"async\" class=\"attachment-600x600 size-600x600\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2017\/08\/single-after-content-widget-area.png\" alt=\"widget area after a single post\" width=\"600\" height=\"390\" \/> <\/div>\n<h2>Adding Widget Areas to Templates Gives You Flexibility<\/h2>\n<p>Widget areas make it possible for your theme&#8217;s users to add extra content without having to write code. But they&#8217;re not just for the sidebar. Adding extra widget areas to your template files gives you targeted areas in your site that you can add widgets to, with fine control over where they&#8217;ll show up.<\/p>\n<p>You can take these examples further if you like, adding widget areas to all of your files, or even using different versions of the <em>sidebar.php<\/em> file for different content types. The world is your oyster!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Widgets make WordPress really flexible. By adding widget areas to your themes, you let users insert their own custom content without having to write a line of code. They&#8217;re most common in sidebars and footers, where they&#8217;re often used to hold widgets such as the search widget, latest posts widget, or maybe a custom menu. [&hellip;]<\/p>\n","protected":false},"author":347011,"featured_media":167176,"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":[557,263],"tags":[52],"tutorials_categories":[],"class_list":["post-167141","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development","category-tutorials","tag-widgets"],"_links":{"self":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/167141","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=167141"}],"version-history":[{"count":13,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/167141\/revisions"}],"predecessor-version":[{"id":209624,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/167141\/revisions\/209624"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media\/167176"}],"wp:attachment":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media?parent=167141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/categories?post=167141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tags?post=167141"},{"taxonomy":"tutorials_categories","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tutorials_categories?post=167141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}