WordPress Widgets and Sidebars

Widgets

In WordPress widgets are blocks of content that admin users can add to their site’s widget areas also known as sidebars. WordPress widgets were created to provide an easy way for WordPress admin users to control the design and content of their site without having to code. Most WordPress themes support widgets.

Sidebars

In WordPress a sidebar, widget area or widget-ready area is any region of the page that can contain widgets. The Sidebars theme feature allow admin users of a WP site to assign widgets to widget areas defined by a theme.

There is no default widget area in WordPress. How many widget areas exist and where they’re located is defined by the theme. Most WordPress themes offer at least one widget area for a site’s “left sidebar”.

WP widget areas can also hold admin-created navigation menus (although menu locations cannot hold widgets).

Use Widgets and Sidebars in a Theme

To use widgets and sidebars in a theme:

  • Register the Sidebars Theme Feature in the functions.php file.
  • Register each widget area using register_sidebar().
  • Display each widget area using dynamic_sidebar() in template files.

Enable Theme Support for Sidebars

Register a Widget Area

Display a Widget Area

dynamic_sidebar()

get_sidebar()

The get_sidebar() function includes a template file named sidebar.php if it exists from the themes template directory. This function is the equivalent of get_header() and get_footer(), but for sidebar.php.

If you do not have a sidebar.php file then get_sidebar() will output a default set of widgets. This is purely for backwards compatibility purposes, as it used to be how sidebars worked. This behavior is now deprecated and you should properly define your sidebars in your theme.

More Resources