How to get the name and description of a sidebar in theme?

I have a sidebar registered (in functions.php) as:

<?php
register_sidebar( array(
        'name'          => __('Activity Calendar'),
        'id'            => 'activity_calendar_en',
        'before_widget' => '',
        'after_widget'  => '',
        'before_title'  => '',
        'after_title'   => '',
    ));
?>

In my theme I am calling it as:

<?php
if( is_active_sidebar( 'activity_calendar_en' ) ):
    dynamic_sidebar( 'activity_calendar_en' );
endif;
?>

Now my question is: how to print the sidebar name (Activity Calendar in this case) in the theme? Is there any method to get and print the sidebar name (as well as any description if available)?

1 Answer
1

You can use the $wp_registered_sidebars global variable. Like this:

global $wp_registered_sidebars;
if( is_active_sidebar( 'activity_calendar_en' ) ):
    esc_html_e( $wp_registered_sidebars['activity_calendar_en']['name'] );
    dynamic_sidebar( 'activity_calendar_en' );
endif;

If you check the WordPress core for is_registered_sidebar function, you’ll see that it’s using this global variable too:

function is_registered_sidebar( $sidebar_id ) {
    global $wp_registered_sidebars;

    return isset( $wp_registered_sidebars[ $sidebar_id ] );
}

However, as far as I know, you can’t retrieve it using any function.

Although, for description there is a function: wp_sidebar_description(). You may use the global variable for description as well, or this function:

if( is_active_sidebar( 'activity_calendar_en' ) ):
    echo wp_sidebar_description( 'activity_calendar_en' );
    dynamic_sidebar( 'activity_calendar_en' );
endif;

Leave a Comment