Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Created August 27, 2012 13:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tommcfarlin/3488292 to your computer and use it in GitHub Desktop.
Save tommcfarlin/3488292 to your computer and use it in GitHub Desktop.
A simple example of how to add a basic sidebar to a WordPress theme
/**
* Registers a new sidebar with the theme.
*
* Note: replace 'themename' with the name of your theme.
*/
function themename_add_sidebar() {
/*
* Description of the parameter array is as follows:
*
* name: The text that appears in the widgetized area in the Dahsboard
* id: The ID of the sidebar. It's a good practice to use sidebar-0, sidebar-1, etc to maintain compatibility so that widgets maintain their position across themes.
* description: The text that appears on the inside of the widgetized area in the Dashboard
* before_widget: The markup written to the front end that starts the sidebar.
* after_widget: The markup written to the front end that ends the sidebar.
* before_title: The markup to display before the widget's title text.
* after_title: The markup to display after the widget's title text.
*
*/
register_sidebar(
array(
'name' => 'Sidebar Title',
'id' => 'sidebar-0',
'description' => 'The primary sidebar',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>'
)
);
}
add_action( 'widgets_init', 'themename_add_sidebar' );
<?php // If there's a widget in the 'Sidebar, display it ?>
<?php if ( is_active_sidebar( 'sidebar-0' ) ) { ?>
<div id="widget">
<?php dynamic_sidebar( 'sidebar-0' ); ?>
</div><!-- /#header-widget -->
<?php // Otherwise, take some other action like writing out default text or markup ?>
<?php } else { ?>
<!-- Write out whatever markup you want. -->
<?php } // end if ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment