{"id":122868,"date":"2013-10-21T11:30:42","date_gmt":"2013-10-21T15:30:42","guid":{"rendered":"http:\/\/wpmu.org\/?p=122868"},"modified":"2014-07-10T21:49:50","modified_gmt":"2014-07-11T01:49:50","slug":"how-to-add-side-menus-to-a-wordpress-theme","status":"publish","type":"post","link":"https:\/\/wpmudev.com\/blog\/how-to-add-side-menus-to-a-wordpress-theme\/","title":{"rendered":"How To Add Side Menus To A WordPress Theme"},"content":{"rendered":"<p>You&#8217;ve most likely seen side menus on a mobile theme. You tap on an icon and the menu slides across from the left or right; tap on the icon again and it slides back.<\/p>\n<p>Whilst a staple of menu theme design, side menus offer something for all themes.<\/p>\n<p>And they are dead easy to implement. I&#8217;ll show you how.<\/p>\n<figure id=\"attachment_122876\" class=\"wp-caption aligncenter\" data-caption=\"true\"><a rel=\"lightbox[122868]\" class=\"blog-thumbnail\" href=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2013\/10\/sidr1.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"size-ratio-large wp-image-122876\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2013\/10\/sidr1-700x170.jpg\" alt=\"Composite image of WordPress 2013 theme heading and Sidr images\" width=\"700\" height=\"170\" \/><\/a><figcaption class=\"wp-caption-text\">Side menus have applications for all themes, not just tablets and mobiles<\/figcaption><\/figure>\n<p>To implement side menus, I&#8217;m going to stand on the shoulders of Alberto Valero and his <a title=\"Visit Alberto's Sidr jQuery plugin page\" href=\"http:\/\/www.berriart.com\/sidr\/\" rel=\"noopener\" target=\"_blank\">Sidr<\/a> plugin for jQuery.<\/p>\n<p>Sidr has all the smarts to easily implement left and right menus. Its building blocks are:<\/p>\n<ul>\n<li>a DIV containing the HTML for the side menu<\/li>\n<li>a link with the <em>href<\/em> set to the side-menu DIV&#8217;s <em>id<\/em> &#8211; this toggles the display of the side menu, that is showing and hiding the menu<\/li>\n<li>Javascript to hook the Sidr functionality to the link<\/li>\n<\/ul>\n<p>Sidr is capable of handling multiple side menus both on the left and right. When a menu is shown, the main content slides in the same direction. So, clicking on the link for a right-hand menu will slide &#8220;out&#8221; the menu to the left and scroll the main content left.<\/p>\n<p>I&#8217;m going to walk you through adding a left and right side menu to the Twenty Thirteen theme just so you can see how it works. Once you know how, you can get creative with them.<\/p>\n<p><strong>UPDATE<\/strong>: I&#8217;ve now included an extended plugin at the bottom of the article that will automatically add two new widget-capable sidebars, so all you need do is add the links to toggle the display to your theme.<\/p>\n<p>Here&#8217;s what we are going to go through:<\/p>\n<ol>\n<li>Add the Sidr library to WordPress<\/li>\n<li>Add the HTML for the side menus<\/li>\n<li>Add a couple of links to toggle the display of the side menus<\/li>\n<li>Add the Javascript to hook up the Sidr functionality<\/li>\n<\/ol>\n<p>Step 1 will be done with a plugin whilst Steps 2 to 4 will be updating the theme.\u00a0Remember, if you want to keep your original theme safe, make a <a title=\"Learn more about Child Themes at the WordPress Codex\" href=\"http:\/\/codex.wordpress.org\/Child_Themes\" rel=\"noopener\" target=\"_blank\">child theme<\/a> and add the changes to the child theme.<\/p>\n<p>Okay, let&#8217;s get started.<\/p>\n<h3>Step 1 &#8211; Install the Plugin<\/h3>\n<p><a href=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2013\/10\/sidr-plugin.zip\" target=\"_blank\">Download and save the plugin<\/a>\u00a0to your computer and then upload it on your site&#8217;s <em>Plugins &gt; Add New<\/em> page. Once loaded, activate it.<\/p>\n<p>All the plugin does is add the Sidr javascript files and the default CSS to each of your website&#8217;s pages.<\/p>\n<p>The Side default CSS is added above the theme CSS, so if you need to change any of the Sidr styles you can do so by adding your own to the theme CSS.<\/p>\n<h3>Step 2 &#8211; Add the HTML for the Side Menus<\/h3>\n<p>To keep things nice and simple for now, we are just going to hook into the<em> wp_footer<\/em> action and add some static code to the footer.<\/p>\n<p>Open up <em>functions.php<\/em> and add the following code:<\/p>\n<p><code><\/code><\/p>\n<p>function sidr_footer() {<\/p>\n<p>\/\/ output the static html for the side menus<\/p>\n<p>echo &#8216; &lt;div id=&#8221;sidr-left&#8221;&gt;<br \/>\n&lt;ul&gt;<br \/>\n&lt;li&gt;&lt;a href=&#8221;#&#8221;&gt;Link 1&lt;\/a&gt;&lt;\/li&gt;<br \/>\n&lt;li&gt;&lt;a href=&#8221;#&#8221;&gt;Link 2&lt;\/a&gt;&lt;\/li&gt;<br \/>\n&lt;li&gt;&lt;a href=&#8221;#&#8221;&gt;Link 3&lt;\/a&gt;&lt;\/li&gt;<br \/>\n&lt;li&gt;&lt;a href=&#8221;#&#8221;&gt;Link 4&lt;\/a&gt;&lt;\/li&gt;<br \/>\n&lt;\/ul&gt;<br \/>\n&lt;\/div&gt;<br \/>\n&lt;div id=&#8221;sidr-right&#8221;&gt;<br \/>\n&lt;ul&gt;<br \/>\n&lt;li&gt;&lt;a href=&#8221;#&#8221;&gt;Link 1&lt;\/a&gt;&lt;\/li&gt;<br \/>\n&lt;li&gt;&lt;a href=&#8221;#&#8221;&gt;Link 2&lt;\/a&gt;&lt;\/li&gt;<br \/>\n&lt;li&gt;&lt;a href=&#8221;#&#8221;&gt;Link 3&lt;\/a&gt;&lt;\/li&gt;<br \/>\n&lt;li&gt;&lt;a href=&#8221;#&#8221;&gt;Link 4&lt;\/a&gt;&lt;\/li&gt;<br \/>\n&lt;\/ul&gt;<br \/>\n&lt;\/div&gt;&#8217;;<\/p>\n<p>}<\/p>\n<p>add_action( &#8216;wp_footer&#8217; , &#8216;sidr_footer&#8217; );<\/p>\n<p>&nbsp;<\/p>\n<p>This simply inserts the HTML for two very basic side menus just before the <em>&lt;body&gt;<\/em> tag.<\/p>\n\n<h3>Step 3 &#8211; Add Side Menu Toggle Links<\/h3>\n<p>These links are purely for demonstration purposes. For a real theme, you&#8217;ll want to integrate them properly into your theme but here we just need to get them onto the page.<\/p>\n<p>Go back to <em>functions.php<\/em> and add the following below the HTML code in the <em>sidr_footer<\/em> function that you added in Step 2:<\/p>\n<p><code><\/code><\/p>\n<p>\/* output the two links *\/<\/p>\n<p>&lt;a href=&#8221;#sidr-left&#8221; class=&#8221;sidr-left-link&#8221;&gt;LEFT&lt;\/a&gt;<br \/>\n&lt;a href=&#8221;#sidr-right&#8221; class=&#8221;sidr-right-link&#8221;&gt;RIGHT&lt;\/a&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>Notice that the <em>href<\/em> attributes on the link match the <em>class<\/em>es for the side menu DIVs.<\/p>\n<p>Originally, this script used ids but using classes enables other elements to control the appearance of the menus &#8211; having a close button in the menu itself, for example.<\/p>\n<p>Although we are not aiming for any artistic prizes, we still want to style these links so that we can at least see them properly.<\/p>\n<p>Open up <em>style.css<\/em> and add the following to the bottom:<\/p>\n<p><code><\/code><\/p>\n<p>\/* sidr links style *\/<\/p>\n<p>.sidr-left-link {<br \/>\ndisplay: block;<br \/>\nbackground: #ffffff;<br \/>\nposition: absolute;<br \/>\ntop: 30px;<br \/>\nleft: 20px;<br \/>\npadding: 10px;<br \/>\nborder: 2px solid #000000;<br \/>\ncolor: #000000;<br \/>\nfont-weight: bold;<br \/>\n}<\/p>\n<p>.sidr-right-link {<br \/>\ndisplay: block;<br \/>\nbackground: #ffffff;<br \/>\nposition: absolute;<br \/>\ntop: 30px;<br \/>\nright: 20px;<br \/>\npadding: 10px;<br \/>\nborder: 2px solid #000000;<br \/>\ncolor: #000000;<br \/>\nfont-weight: bold;<br \/>\n}<\/p>\n<p>&nbsp;<\/p>\n<h3>Step 4 &#8211; Hook Up the Sidr Functionality<\/h3>\n<p>Our last step is to hook up the links so that clicking on them toggles the display of the side menus.<\/p>\n<p>Again, go back to the <em>functions.php<\/em> and add this code underneath the HTML for the two links that you created in Step 3:<\/p>\n<p><code><\/code><\/p>\n<p>\/* hook up the Sidr functionality *\/<\/p>\n<p>&lt;script language=&#8221;javascript&#8221;&gt;<\/p>\n<p>jQuery(document).ready(function($){<br \/>\n\/\/ hook up the left side menu<\/p>\n<p>$(&#8220;.sidr-left-link&#8221;).sidr({<br \/>\nname: &#8220;sidr-left&#8221;,<br \/>\nside: &#8220;left&#8221;<br \/>\n});<\/p>\n<p>\/\/ hook up the right side menu<br \/>\n$(&#8220;.sidr-right-link&#8221;).sidr({<br \/>\nname: &#8220;sidr-right&#8221;,<br \/>\nside: &#8220;right&#8221;<br \/>\n});<\/p>\n<p>});<\/p>\n<p>&lt;\/script&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>Here you can see that <em>sidr<\/em> function is being attached to each link. We tell the <em>sidr<\/em> function what the <em>id<\/em> of the DIV is (<em>sidr-left<\/em> and <em>sidr-right<\/em>) and which direction it is sliding from.<\/p>\n<p>That&#8217;s all there is. Open your site in a browser and you should see two large white boxes in the header with LEFT and RIGHT. Click on them to toggle the display of the side menus.<\/p>\n\n<h2>Actually Making Them Useful<\/h2>\n<p>This demo is pretty bland but it&#8217;s not hard to see how useful side menus could be especially for tablets and small devices.<\/p>\n<h3>Adding Dynamic Menus<\/h3>\n<p>To properly use them for menus, you want to output the menu dynamically by using a call to <em>wp_nav_menu<\/em> referencing a menu that you can manage in the admin section.<\/p>\n<p>Replace the<em> sidr-left<\/em> code you added in Step 2 with this code to output the <em>primary<\/em> menu from Twenty Thirteen<\/p>\n<p><code><\/code><\/p>\n<p>echo &#8216; &lt;div id=&#8221;sidr-left&#8221;&gt; &#8216;;<br \/>\nwp_nav_menu( array( &#8216;theme_location&#8217; =&gt; &#8216;primary&#8217; ) );<br \/>\necho &#8216; &lt;\/div&gt; &#8216;;<\/p>\n<p>&nbsp;<\/p>\n<p>If you want to display an entirely new menu then you need to register it first. Add this code to the <em>functions.php<\/em><\/p>\n<p><code><\/code><\/p>\n<p>function sidr_new_menu() {<\/p>\n<p>\/\/ This theme uses wp_nav_menu() in one location.<br \/>\nregister_nav_menu( &#8216;side-menu-left&#8217;, __( &#8216;Sidebar Left Menu&#8217;, &#8216;twentythirteen&#8217; ) );<\/p>\n<p>}<\/p>\n<p>add_action ( &#8216;after_theme_setup&#8217; , &#8216;sidr_new_menu&#8217;);<\/p>\n<p>&nbsp;<\/p>\n<p>This will create a new menu location in the <em>Appearance &gt; Menus<\/em> admin interface where you can add your links.<\/p>\n<p>Change the <em>theme_location<\/em> in the call to <em>wp_nav_menu<\/em> to <em>side-menu-left<\/em> to output the new menu.<\/p>\n<h3>Adding New Widget-Capable Sidebars<\/h3>\n<p>There&#8217;s no reason why you cannot use a new sidebar as a side menu with all the benefits that come from being able to use widgets.<\/p>\n<p>To set up a new sidebar add the following to your <em>functions.php<\/em>:<\/p>\n<p><code><\/code><\/p>\n<p>function sidr_widgets_init() {<\/p>\n<p>register_sidebar( array(<br \/>\n&#8216;name&#8217; =&gt; __( &#8216;Right Hand Sidebar&#8217;, &#8216;twentythirteen&#8217; ),<br \/>\n&#8216;id&#8217; =&gt; &#8216;sidr-right&#8217;,<br \/>\n&#8216;description&#8217; =&gt; __( &#8216;Right Hand Side Menu&#8217;, &#8216;twentythirteen&#8217; ),<br \/>\n&#8216;before_widget&#8217; =&gt; &#8216;&lt;aside id=&#8221;%1$s&#8221; class=&#8221;widget %2$s&#8221;&gt;&#8217;,<br \/>\n&#8216;after_widget&#8217; =&gt; &#8216;&lt;\/aside&gt;&#8217;,<br \/>\n&#8216;before_title&#8217; =&gt; &#8216;&lt;h3 class=&#8221;widget-title&#8221;&gt;&#8217;,<br \/>\n&#8216;after_title&#8217; =&gt; &#8216;&lt;\/h3&gt;&#8217;,<br \/>\n) );<\/p>\n<p>}<\/p>\n<p>add_action( &#8216;widgets_init&#8217; , &#8216;sidr_widgets_init&#8217; );<\/p>\n<p>&nbsp;<\/p>\n<p>You should now see a new Right Hand Side Menu location in <em>Appearance &gt; Widgets<\/em> admin screen. Add a couple of test widgets to the sidebar and then back in <em>functions.php<\/em> replace the <em>sidr-right<\/em> HTML that you added in Step 3 with the following:<\/p>\n<p><code><\/code><\/p>\n<p>if ( is_active_sidebar( &#8216;sidr-right&#8217; ) ) :<br \/>\necho &#8216; &lt;div id=&#8221;sidr-right&#8221; class=&#8221;sidebar-container&#8221; role=&#8221;complementary&#8221;&gt;<br \/>\n&lt;div class=&#8221;widget-area&#8221;&gt;&#8217;;<br \/>\ndynamic_sidebar( &#8216;sidr-right&#8217; );<br \/>\necho &#8216; &lt;\/div&gt;&lt;!&#8211; .widget-area &#8211;&gt;<br \/>\n&lt;\/div&gt;&lt;!&#8211; #sidr-right &#8211;&gt;&#8217;;<br \/>\nendif;<\/p>\n<p>&nbsp;<\/p>\n<p>Refresh your site and click on the <em>LEFT<\/em> and <em>RIGHT<\/em> links to discover side menus with left-hand menus and right-hand widgetized sidebars.<\/p>\n<p>That&#8217;s just a couple of examples of how side menus could be used. I&#8217;m sure that there are plenty more you can think of, especially for smaller devices.<\/p>\n<p>With Alberto&#8217;s code side menus are really very easy to add to any theme, so if you can see a use case for them, there&#8217;s really nothing to stop you from implementing them.<\/p>\n<p>&nbsp;<\/p>\n<p>Image credit:\u00a0<a title=\"Visit Alberto's site\" href=\"http:\/\/www.berriart.com\/\" rel=\"noopener\" target=\"_blank\">Alberto Valero<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You&#8217;ve most likely seen side menus on a mobile theme. You tap on an icon and the menu slides across from the left or right; tap on the icon again and it slides back. Side menus offer something for all themes, though, and they are dead easy to implement. <\/p>\n","protected":false},"author":262394,"featured_media":122876,"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":[38,1044,87],"tutorials_categories":[],"class_list":["post-122868","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-design","tag-css","tag-menus"],"_links":{"self":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/122868","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\/262394"}],"replies":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/comments?post=122868"}],"version-history":[{"count":2,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/122868\/revisions"}],"predecessor-version":[{"id":198637,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/122868\/revisions\/198637"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media\/122876"}],"wp:attachment":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media?parent=122868"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/categories?post=122868"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tags?post=122868"},{"taxonomy":"tutorials_categories","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tutorials_categories?post=122868"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}