snipt

Ctrl+h for KB shortcuts

PHP

WordPress: function to add sidebar into theme

/* ADD SIDEBAR */
// Register Sidebars
function asa_sidebar() {

	$args = array(
		'id'            => 'main-sidebar',
		'class'         => 'main-sidebar',
		'name'          => __( 'Main Sidebar', 'text_domain' ),
		'description'   => __( 'Appears beneath sub-page navigation.', 'text_domain' ),
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget'  => '</div>',
	);
	register_sidebar( $args );

}
add_action( 'widgets_init', 'asa_sidebar' );

Description

WordPress code for functions.php to add a sidebar to your theme.
https://snipt.net/embed/8cec62de2c110a5aa186514e846e4a39/
/raw/8cec62de2c110a5aa186514e846e4a39/
8cec62de2c110a5aa186514e846e4a39
php
PHP
20
2019-06-20T06:05:41
True
False
False
Oct 13, 2016 at 04:08 PM
/api/public/snipt/147991/
wordpress-function-to-add-sidebar-into-theme
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1"> 1</a> <a href="#L-2"> 2</a> <a href="#L-3"> 3</a> <a href="#L-4"> 4</a> <a href="#L-5"> 5</a> <a href="#L-6"> 6</a> <a href="#L-7"> 7</a> <a href="#L-8"> 8</a> <a href="#L-9"> 9</a> <a href="#L-10">10</a> <a href="#L-11">11</a> <a href="#L-12">12</a> <a href="#L-13">13</a> <a href="#L-14">14</a> <a href="#L-15">15</a> <a href="#L-16">16</a> <a href="#L-17">17</a> <a href="#L-18">18</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="x">/* ADD SIDEBAR */</span> </span><span id="L-2"><a name="L-2"></a><span class="x">// Register Sidebars</span> </span><span id="L-3"><a name="L-3"></a><span class="x">function asa_sidebar() {</span> </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a><span class="x"> $args = array(</span> </span><span id="L-6"><a name="L-6"></a><span class="x"> &#39;id&#39; =&gt; &#39;main-sidebar&#39;,</span> </span><span id="L-7"><a name="L-7"></a><span class="x"> &#39;class&#39; =&gt; &#39;main-sidebar&#39;,</span> </span><span id="L-8"><a name="L-8"></a><span class="x"> &#39;name&#39; =&gt; __( &#39;Main Sidebar&#39;, &#39;text_domain&#39; ),</span> </span><span id="L-9"><a name="L-9"></a><span class="x"> &#39;description&#39; =&gt; __( &#39;Appears beneath sub-page navigation.&#39;, &#39;text_domain&#39; ),</span> </span><span id="L-10"><a name="L-10"></a><span class="x"> &#39;before_title&#39; =&gt; &#39;&lt;h3 class=&quot;widget-title&quot;&gt;&#39;,</span> </span><span id="L-11"><a name="L-11"></a><span class="x"> &#39;after_title&#39; =&gt; &#39;&lt;/h3&gt;&#39;,</span> </span><span id="L-12"><a name="L-12"></a><span class="x"> &#39;before_widget&#39; =&gt; &#39;&lt;div id=&quot;%1$s&quot; class=&quot;widget %2$s&quot;&gt;&#39;,</span> </span><span id="L-13"><a name="L-13"></a><span class="x"> &#39;after_widget&#39; =&gt; &#39;&lt;/div&gt;&#39;,</span> </span><span id="L-14"><a name="L-14"></a><span class="x"> );</span> </span><span id="L-15"><a name="L-15"></a><span class="x"> register_sidebar( $args );</span> </span><span id="L-16"><a name="L-16"></a> </span><span id="L-17"><a name="L-17"></a><span class="x">}</span> </span><span id="L-18"><a name="L-18"></a><span class="x">add_action( &#39;widgets_init&#39;, &#39;asa_sidebar&#39; );</span> </span></pre></div> </td></tr></table>
function, php, sidebar, wordpress
--- 
+++ 
@@ -1,13 +1,19 @@
 /* ADD SIDEBAR */
-add_action( 'widgets_init', 'asa_theme_sidebar_init' );
-function asa_theme_sidebar_init() {
-    register_sidebar( array(
-        'name' => __( 'Main Sidebar', 'asa-theme' ),
-        'id' => 'sidebar-1',
-        'description' => __( 'Widgets in this area will be shown below sub-page navigation.', 'asa-theme' ),
-        'before_widget' => '<li id="%1$s" class="widget %2$s">',
-	'after_widget'  => '</li>',
-	'before_title'  => '<h2 class="widget-title">',
-	'after_title'   => '</h2>',
-    ) );
+// Register Sidebars
+function asa_sidebar() {
+
+	$args = array(
+		'id'            => 'main-sidebar',
+		'class'         => 'main-sidebar',
+		'name'          => __( 'Main Sidebar', 'text_domain' ),
+		'description'   => __( 'Appears beneath sub-page navigation.', 'text_domain' ),
+		'before_title'  => '<h3 class="widget-title">',
+		'after_title'   => '</h3>',
+		'before_widget' => '<div id="%1$s" class="widget %2$s">',
+		'after_widget'  => '</div>',
+	);
+	register_sidebar( $args );
+
 }
+add_action( 'widgets_init', 'asa_sidebar' );
+
--- 
+++ 
@@ -0,0 +1,13 @@
+/* ADD SIDEBAR */
+add_action( 'widgets_init', 'asa_theme_sidebar_init' );
+function asa_theme_sidebar_init() {
+    register_sidebar( array(
+        'name' => __( 'Main Sidebar', 'asa-theme' ),
+        'id' => 'sidebar-1',
+        'description' => __( 'Widgets in this area will be shown below sub-page navigation.', 'asa-theme' ),
+        'before_widget' => '<li id="%1$s" class="widget %2$s">',
+	'after_widget'  => '</li>',
+	'before_title'  => '<h2 class="widget-title">',
+	'after_title'   => '</h2>',
+    ) );
+}