{"id":1631,"date":"2019-07-13T17:15:52","date_gmt":"2019-07-13T09:15:52","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=1631"},"modified":"2020-05-12T11:00:36","modified_gmt":"2020-05-12T03:00:36","slug":"how-to-allow-arbitrary-widget-content-to-be-added-to-sections-of-your-wordpress-theme","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-allow-arbitrary-widget-content-to-be-added-to-sections-of-your-wordpress-theme\/","title":{"rendered":"How to allow arbitrary Widget content to be added to sections of your WordPress theme"},"content":{"rendered":"<p>When you use <a href=\"https:\/\/wordpress.org\/\" rel=\"noopener\" target=\"_blank\">WordPress<\/a> to house your thoughts, you get the flexibility to update sections of your WordPress site through the <a href=\"https:\/\/en.support.wordpress.com\/dashboard\/\" rel=\"noopener\" target=\"_blank\">WordPress dashboard<\/a>.<\/p>\n<p>For example, I can change the content within the <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-make-a-portion-of-your-sidebar-sticky-as-you-scroll-till-the-footer-of-your-web-page-with-jquery-and-some-css-styles\/\" rel=\"noopener\" target=\"_blank\">sticky portion of my sidebar<\/a> as and when I need to. Such content can be provided through the various <a href=\"https:\/\/en.support.wordpress.com\/widgets\/\" rel=\"noopener\" target=\"_blank\">WordPress Widgets<\/a> that are available in my WordPress installation.<\/p>\n<p>So how can we mark sections of our <a href=\"https:\/\/wordpress.org\/support\/article\/using-themes\/#what-is-a-theme\" rel=\"noopener\" target=\"_blank\">WordPress themes<\/a> for displaying Widget content?<\/p>\n<p>With this in mind, let us look at how we can allow arbitrary Widget content to be added to sections of our WordPress theme via the WordPress dashboard.<\/p>\n<h2>Creating sections for Widget content to appear in<\/h2>\n<p>For the purpose of this article, let's assume that we want to <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-make-a-portion-of-your-sidebar-sticky-as-you-scroll-till-the-footer-of-your-web-page-with-jquery-and-some-css-styles\/\" rel=\"noopener\" target=\"_blank\">create a section in the sidebar that will stick till the browser reaches footer<\/a>. In addition to that, we also want to have the flexibility to add arbitrary content before that sticky portion.<\/p>\n<p>Given these points, we can include the following codes in our theme's <a href=\"https:\/\/codex.wordpress.org\/Functions_File_Explained\" rel=\"noopener\" target=\"_blank\"><strong><code>functions.php<\/code><\/strong><\/a>:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfunction my_widgets_init() {\r\n  register_sidebar( array(\r\n\t\t'name'          =&gt; 'Before sticky sidebar content',\r\n\t\t'id'            =&gt; 'before_sticky_sidebar_content',\r\n\t\t'before_widget' =&gt; '&lt;section&gt;',\r\n\t\t'after_widget'  =&gt; '&lt;\/section&gt;',\r\n\t\t'before_title'  =&gt; '&lt;h3&gt;',\r\n\t\t'after_title'   =&gt; '&lt;\/h3&gt;',\r\n\t) );\r\n  register_sidebar( array(\r\n\t\t'name'          =&gt; 'Sticky sidebar content',\r\n\t\t'id'            =&gt; 'sticky_sidebar_content',\r\n\t\t'before_widget' =&gt; '&lt;section&gt;',\r\n\t\t'after_widget'  =&gt; '&lt;\/section&gt;',\r\n\t\t'before_title'  =&gt; '&lt;h3&gt;',\r\n\t\t'after_title'   =&gt; '&lt;\/h3&gt;',\r\n\t) );\r\n\r\n}\r\nadd_action( 'widgets_init', 'my_widgets_init' );\r\n<\/pre>\n<p>So what is the above code trying to achieve?<\/p>\n<p>First, we define a function that will call the <a href=\"https:\/\/codex.wordpress.org\/Widgets_API\/register_sidebar\" rel=\"noopener\" target=\"_blank\"><code>register_sidebar<\/code><\/a> function to mark sections for Widget content to appear in. For each function call, we pass in an associative <a href=\"https:\/\/www.php.net\/manual\/en\/language.types.array.php\" rel=\"noopener\" target=\"_blank\">array<\/a> that define the configurations for marking the section for Widgets and displaying of Widgets within that section.<\/p>\n<p>After we had defined the function, we call <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/add_action\/\" rel=\"noopener\" target=\"_blank\"><code>add_action<\/code><\/a> to hook <code>my_widgets_init<\/code> function to the <a href=\"https:\/\/developer.wordpress.org\/reference\/hooks\/widgets_init\/\" rel=\"noopener\" target=\"_blank\"><code>widgets_init<\/code><\/a> event.<\/p>\n<p>Once WordPress had taken the changes that we had made to our <strong><code>functions.php<\/code><\/strong> file, we should see our <strong>Before sticky sidebar content<\/strong> and <strong>Sticky sidebar content<\/strong> in <strong>Appearance -> Widgets<\/strong> of our WordPress dashboard:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/WordPress-Dashboard-Widgets-Screen-showing-Before-sticky-sidebar-content-and-Sticky-sidebar-content.gif\" alt=\"WordPress Dashboard Widgets Screen showing Before sticky sidebar content and Sticky sidebar content\" \/><\/p>\n<h2>Displaying the Widgets added to the marked sections<\/h2>\n<p>Once you had indicated to WordPress the sections that you allow for arbitrary content to be added, you decide where to display them.<\/p>\n<p>In order to display the arbitrary content that was added to the sections that we had marked earlier, we can write codes similar to the following:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n  if (is_active_sidebar('before_sticky_sidebar_content')) {\r\n?&gt;\r\n  &lt;section id=&quot;before-sticky-sidebar&quot;&gt;\r\n&lt;?php\r\n\t\tdynamic_sidebar('before_sticky_sidebar_content');\r\n?&gt;\r\n  &lt;\/section&gt;\r\n&lt;?php\r\n  } \/\/ end if (is_active_sidebar('before_sticky_sidebar_content'))\r\n  if (is_active_sidebar('sticky_sidebar_content')) {\r\n?&gt;\r\n  &lt;section id=&quot;sticky-sidebar&quot;&gt;\r\n&lt;?php\r\n\t\tdynamic_sidebar('sticky_sidebar_content');\r\n?&gt;\r\n  &lt;\/section&gt;\r\n&lt;?php\r\n  } \/\/ end if (is_active_sidebar('sticky_sidebar_content'))\r\n?&gt;\r\n<\/pre>\n<p>In this case, we use the <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/is_active_sidebar\/\" rel=\"noopener\" target=\"_blank\"><code>is_active_sidebar<\/code><\/a> function to check if there are any Widgets added to the section that we had marked. When there are Widgets added via the WordPress dashboard, we use the <a href=\"https:\/\/codex.wordpress.org\/Function_Reference\/dynamic_sidebar\" rel=\"noopener\" target=\"_blank\"><code>dynamic_sidebar<\/code><\/a> to display them. <\/p>\n<p>As an input to both functions, we use the <strong><code>id<\/code><\/strong> value that was supplied to the <code>register_sidebar<\/code> function to identify the different sections for arbitrary content to appear. <\/p>\n<h2>Adding Widgets to the sidebars<\/h2>\n<p>Once you had the coding in place, you can then add Widgets to your sidebars. <\/p>\n<p>In order to do so, go to the <strong>Widgets<\/strong> page of your WordPress dashboard. Once you are in the <strong>Widgets<\/strong> page, simply drag any Widget from the <strong>Available Widgets<\/strong> section to the sidebars that you had created. <\/p>\n<p>So how will the Widget be displayed?<\/p>\n<p>The content of the Widgets will be displayed in order from top to bottom. For example, in the following configuration, the <strong>Ad Inserter<\/strong> widget will be displayed before the two <strong>Custom HTML<\/strong> widgets:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Example-Widgets-content-that-are-added-to-Before-sticky-sidebar-content-and-Sticky-sidebar-content.gif\" alt=\"Example Widgets content that are added to Before sticky sidebar content and Sticky sidebar content\"\/><\/p>\n<h2>Reordering Widgets within a sidebar<\/h2>\n<p>Whenever you feel like reordering the Widgets within a sidebar, simply drag a Widget to the top or bottom of another Widget.<\/p>\n<h2>Adding advertising HTML \/ JavaScript codes inside your sidebar<\/h2>\n<p>When I want to try out different <a href=\"https:\/\/www.techcoil.com\/blog\/easy-and-effective-ways-for-programmers-websites-to-earn-money\/\" rel=\"noopener\" target=\"_blank\">ways for programmers\u2019 websites to earn money<\/a>, I can use the <a href=\"https:\/\/en.support.wordpress.com\/widgets\/custom-html-widget\/\" rel=\"noopener\" target=\"_blank\">Custom HTML widget<\/a> to display some HTML \/ JavaScript codes in the sticky portion of my sidebar.<\/p>\n<p>In order to do so, I drag an instance of Custom HTML Widget to a sidebar and paste the HTML \/ JavaScript codes for displaying an advertisement.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Google-Adsense-code-example-as-a-Custom-HTML-Widget.gif\" alt=\"Google Adsense code example as a Custom HTML Widget\"\/ class=\"aligncenter\"><\/p>\n<p>Since I do not want to give a title to the Widget, I left it as blank. When I am satisfied with the content, I click on <strong>Save<\/strong> to make the advertisement unit appear in my sidebar.<\/p>\n<h2>Removing Widgets from a sidebar<\/h2>\n<p>When you want to remove a Widget from a sidebar, simply drag the Widget out of the sidebar. Another way to do so is to click into the Widget that you wish to remove and click on <strong>Delete<\/strong>.<\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-qj\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-qj&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-qj&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1631&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>When you use <a href=\"https:\/\/wordpress.org\/\" rel=\"noopener\" target=\"_blank\">WordPress<\/a> to house your thoughts, you get the flexibility to update sections of your WordPress site through the <a href=\"https:\/\/en.support.wordpress.com\/dashboard\/\" rel=\"noopener\" target=\"_blank\">WordPress dashboard<\/a>.<\/p>\n<p>For example, I can change the content within the <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-make-a-portion-of-your-sidebar-sticky-as-you-scroll-till-the-footer-of-your-web-page-with-jquery-and-some-css-styles\/\" rel=\"noopener\" target=\"_blank\">sticky portion of my sidebar<\/a> as and when I need to. Such content can be provided through the various <a href=\"https:\/\/en.support.wordpress.com\/widgets\/\" rel=\"noopener\" target=\"_blank\">WordPress Widgets<\/a> that are available in my WordPress installation.<\/p>\n<p>So how can we mark sections of our <a href=\"https:\/\/wordpress.org\/support\/article\/using-themes\/#what-is-a-theme\" rel=\"noopener\" target=\"_blank\">WordPress themes<\/a> for displaying Widget content?<\/p>\n<p>With this in mind, let us look at how we can allow arbitrary Widget content to be added to sections of our WordPress theme via the WordPress dashboard.<\/p>\n","protected":false},"author":1,"featured_media":1633,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[659,662,459,16,661,660,663,5,170,658],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/WordPress-Dashboard-Widgets-Screen-showing-Before-sticky-sidebar-content-and-Sticky-sidebar-content.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-qj","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1631"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=1631"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1631\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1633"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=1631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=1631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=1631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}