Skip to content

Instantly share code, notes, and snippets.

@dotherightthing
Last active January 21, 2021 22:04
Show Gist options
  • Save dotherightthing/544e22e737587bde04d3a77b46da5796 to your computer and use it in GitHub Desktop.
Save dotherightthing/544e22e737587bde04d3a77b46da5796 to your computer and use it in GitHub Desktop.
Register a widget-ready sidebar #wordpress #cheatsheet
/**
* Register a widget-ready sidebar
*
* @example
* // functions.php
* add_action( 'widgets_init', 'wpdtrt_widgets_init' );
*
* // mytheme/sidebar-location.php
* if ( is_active_sidebar( 'sidebar-location' ) ) {
* dynamic_sidebar( 'sidebar-location' );
* }
*
* // single.php
* <?php get_sidebar('location'); ?>
*/
function wpdtrt_widgets_init() {
register_sidebar( array(
'name' => esc_html__( 'Sidebar', 'wpdtrt' ),
'id' => 'sidebar-location',
'description' => esc_html__( 'Add widgets here to appear in the location sidebar.', 'wpdtrt' ),
'before_widget' => '',
'after_widget' => '',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
));
}
/**
* The template for displaying a widget-ready sidebar
* The Theme should be widgetized as fully as possible. Any area in the layout that works like a widget
* (tag cloud, blogroll, list of categories) or could accept widgets (sidebar) should allow widgets.
* Content that appears in widgetized areas by default (hard-coded into the sidebar, for example)
* should disappear when widgets are enabled from Appearance > Widgets.
*
* @see https://codex.wordpress.org/Theme_Development
*/
if ( is_active_sidebar( 'sidebar-location' ) ) {
dynamic_sidebar( 'sidebar-location' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment