I ask if it is possible to wrap a specific sidebar within a <div class="customdiv"></div>. Are there any instructions to add in the function.php file? Thank you.

Like this:

I've created this sidebar (which contains widgets):
    // ATTIVITA - widgets   
        array (
            'name' => __( 'ATTIVITA prenotabili', 'your-theme-domain' ),
            'id' => '<div id="testimonial-slider">',
            'description' => __( 'ATTIVITA prenotabili', 'your-theme-domain' ),
            'before_widget' => '<div class="widget-content">',
            'after_widget' => '</div>',
            'before_title' => '<h3 class="widget-title">',
            'after_title' => '</h3>',

I would like to wrap it into a Do you think could it be possible? Many thanks.

  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Commented Nov 4, 2021 at 5:40

2 Answers 2


UPDATE: now that the post is clarified, in your custom sidebar declaration change these lines to the following (this preserves existing wrappers too, which are advisable to leave in unless you know you want to delete them):

            'before_widget' => '<div class="customdiv"><div class="widget-content">',
            'after_widget' => '</div></div>',

I assume you're trying to edit a system-loaded sidebar. Try this code in functions.php. The sidebar you want to edit with this is going to have an id on the front-end that you can find by using the tag inspector in Developer tools in your browser, and once you have that id you have to replace 'specific_sidebar_id' on the fifth line below with the id of the sidebar you wish to alter.

function customize_my_sidebar() {

    global $wp_registered_sidebars;

    $key = array_search('specific_sidebar_id', array_column($wp_registered_sidebars, 'id'));

    $wp_registered_sidebars[$key]['before_widget'] = '<div class="customdiv">';
    $wp_registered_sidebars[$key]['after_widget'] = '</div>';


add_action( 'wp_loaded', 'customize_my_sidebar' );

If you're registering an entirely new sidebar, you should customize "before_widget" and "after_widget" in your definition when calling register_sidebar() as documented here: https://developer.wordpress.org/reference/functions/register_sidebar/

  • Thank you @BrianVan, i've just edited the post to better understand. Do you think is possible? Many thanks.
    – Filippo
    Commented Nov 4, 2021 at 12:41

From your question I understand you want to wrap the complete sidebar in a div, not the individual widgets inside that sidebar. This is not possible in the registering phase. It is, however, possible in the rendering phase. In your theme this is done with the function dynamic_sidebar.

Now, when you take a look at that function, you will see that there are actions defined just before and after the rendering of the widgets, namely dynamic_sidebar_before and dynamic_sidebar_after. You would use these as follows:

add_action ('dynamic_sidebar_before','wpse397644_before_sidebar');
function wpse397644_before_sidebar ($index, $has_widgets) {
  if ($has_widgets) echo '<div class="customdiv">';

add_action ('dynamic_sidebar_after','wpse397644_after_sidebar');
function wpse397644_after_sidebar ($index, $has_widgets) {
  if ($has_widgets) echo '</div>';
  • Thanks a lot Cjbj, shoudl I put this code into function.php file, right?
    – Filippo
    Commented Nov 4, 2021 at 18:21
  • Yes, in functions.php
    – cjbj
    Commented Nov 4, 2021 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.