Builds the definition for a single sidebar and returns the ID. Call on "widgets_init" action.

register_sidebar() 描述


<?php register_sidebar$args ); ?>

Default Usage

register_sidebar() 用法



(string/array) (可选) Builds Sidebar based off of 'name' and 'id' values.

默认值: None

  • name - Sidebar name (default is localized 'Sidebar' and numeric ID).
  • id - Sidebar id - Must be all in lowercase, with no spaces (default is a numeric auto-incremented ID). If you do not set the id argument value, you will get E_USER_NOTICE messages in debug mode, starting with version 4.2.
  • description - Text description of what/where the sidebar is. Shown on widget management screen. (Since 2.9) (default: empty)
  • class - CSS class to assign to the Sidebar in the Appearance -> Widget admin page. This class will only appear in the source of the WordPress Widget admin page. It will not be included in the frontend of your website. Note: The value "sidebar" will be prepended to the class value. For example, a class of "tal" will result in a class value of "sidebar-tal". (default: empty).
  • before_widget - HTML to place before every widget(default: <li id="%1$s" class="widget %2$s">) Note: uses sprintf for variable substitution
  • after_widget - HTML to place after every widget (default: </li>
  • before_title - HTML to place before every title (default: <h2 class="widgettitle">).
  • after_title - HTML to place after every title (default: </h2>

The optional args parameter is an associative array that will be passed as a first argument to every active widget callback. (If a string is passed instead of an array, it will be passed through parse_str() to generate an associative array.) The basic use for these arguments is to pass theme-specific HTML tags to wrap the widget and its title.

register_sidebar() 参数


  • With WordPress 3.4.1 there're still some IDs to avoid, that can be found here. Props to "toscho" for building a plugin collecting and listing them.
  • Calling register_sidebar() multiple times to register a number of sidebars is preferable to using register_sidebars() to create a bunch in one go, because it allows you to assign a unique name to each sidebar (eg: “Right Sidebar”, “Left Sidebar”). Although these names only appear in the admin interface it is a best practice to name each sidebar specifically, giving the administrative user some idea as to the context for which each sidebar will be used.
  • The default before/after values are intended for themes that generate a sidebar marked up as a list with h2 titles. This is the convention we recommend for all themes and any theme built in this way can simply register sidebars without worrying about the before/after tags. If, for some compelling reason, a theme cannot be marked up in this way, these tags must be specified when registering sidebars. It is recommended to copy the id and class attributes verbatim so that an internal sprintf call can work and CSS styles can be applied to individual widgets.

register_sidebar() 注意


  • 4.2.0 : Missing id property now triggers E_USER_NOTICE.
  • 2.9.0 : Added description property
  • 添加于 版本: 2.2.0

register_sidebar() 历史


register_sidebar() 函数的代码位于 wp-includes/widgets.php.

register_sidebar() 源文件