X

How to Reuse Sidebar Arguments in WordPress

Snippets by IsItWP

Are you looking for a way to reuse sidebar arguments? While there’s probably a plugin for this, we have created a quick code snippet that you can use to reuse sidebar arguments in WordPress.

While there are sites that have a bunch of sidebars, any argument you add to the array in register_sidebar will override the corresponding argument in $default_sidebar_args with the properties of array_merge. If you want to use a Heading 3 for just one sidebar, then that’s easy to do now.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:


$default_sidebar_args = array(
    'before_title' => '<h2 class="widget-title">',
    'after_title' => '</h2>',
    'before_widget' => '<section id="%1$s" class="widget %2$s">',
    'after_widget' => '</section>'
);

// Site-wide sidebar
register_sidebar(
    array_merge( $default_sidebar_args, array(
        'name' => 'My Sidebar',
        'id'   => 'my-sidebar',
        'description'   => 'This is a sidebar.'
    ) )
);
// repeat register-sidebar as many times as you like, only changing, name, ID, and decription.

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 10 best WordPress plugins for web developers and how to save partial form data in WordPress.

Comments   Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!