register_sidebar()
概述
register_sidebar() 函数用于在 WordPress 中注册单个侧边栏,定义其属性并返回侧边栏 ID。它接受参数数组或字符串,并自动处理默认值,如未指定 ID 和名称时会基于已注册侧边栏数量生成。
关键要点
- 函数用于注册侧边栏,返回侧边栏 ID,并自动启用 'widgets' 主题支持(若未启用)。
- 参数包括 name、id、description、class、before_widget、after_widget、before_title、after_title、before_sidebar、after_sidebar 和 show_in_rest,其中 id 必须为小写且无空格。
- 若未提供 id 参数,WordPress 会生成默认 ID(如 'sidebar-$instance'),但自 4.2.0 版本起会触发 _doing_it_wrong() 通知,建议手动设置以避免问题。
- 函数内部使用 wp_parse_args() 合并参数,并应用 register_sidebar_defaults 过滤器,注册后触发 register_sidebar 动作。
- 相关函数包括 register_sidebars()(用于注册多个侧边栏)、add_theme_support()、__()、_doing_it_wrong()、wp_parse_args()、apply_filters() 和 do_action()。
- 版本更新:5.9.0 添加 show_in_rest 参数,5.6.0 添加 before_sidebar 和 after_sidebar 参数,2.2.0 引入函数。
代码示例
/**
* Add a sidebar.
*/
function wpdocs_theme_slug_widgets_init() {
register_sidebar( array(
'name' => __( 'Main Sidebar', 'textdomain' ),
'id' => 'sidebar-1',
'description' => __( 'Widgets in this area will be shown on all posts and pages.', 'textdomain' ),
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
) );
}
add_action( 'widgets_init', 'wpdocs_theme_slug_widgets_init' );注意事项
- 侧边栏 ID 必须为小写且无空格,否则在使用 is_sidebar_active() 或 dynamic_sidebar() 时可能因 sanitize_title() 转换而失败。
- 自动生成的名称和 ID 可能因其他插件或主题安装而改变,建议显式设置以保持稳定性。
- 在 widgets_init 钩子中调用 register_sidebar() 以确保正确注册。
Builds the definition for a single sidebar and returns the ID.
Description
Accepts either a string or an array and then parses that against a set of default arguments for the new sidebar. WordPress will automatically generate a sidebar ID and name based on the current number of registered sidebars if those arguments are not included.
When allowing for automatic generation of the name and ID parameters, keep in mind that the incrementor for your sidebar can change over time depending on what other plugins and themes are installed.
If theme support for ‘widgets’ has not yet been added when this function is called, it will be automatically enabled through the use of add_theme_support() .
Parameters
$argsarray|stringoptional-
Array or string of arguments for the sidebar being registered.
namestringThe name or title of the sidebar displayed in the Widgets interface. Default ‘Sidebar $instance’.idstringThe unique identifier by which the sidebar will be called.
Default'sidebar-$instance'.descriptionstringDescription of the sidebar, displayed in the Widgets interface.classstringExtra CSS class to assign to the sidebar in the Widgets interface.before_widgetstringHTML content to prepend to each widget’s HTML output when assigned to this sidebar. Receives the widget’s ID attribute as%1$sand class name as%2$s. Default is an opening list item element.after_widgetstringHTML content to append to each widget’s HTML output when assigned to this sidebar. Default is a closing list item element.before_titlestringHTML content to prepend to the sidebar title when displayed.
Default is an opening h2 element.after_titlestringHTML content to append to the sidebar title when displayed.
Default is a closing h2 element.before_sidebarstringHTML content to prepend to the sidebar when displayed.
Receives the$idargument as%1$sand$classas%2$s.
Outputs after the ‘dynamic_sidebar_before’ action.after_sidebarstringHTML content to append to the sidebar when displayed.
Outputs before the ‘dynamic_sidebar_after’ action.show_in_restboolWhether to show this sidebar publicly in the REST API.
Defaults to only showing the sidebar to administrator users.
Default:
array()
Source
function register_sidebar( $args = array() ) {
global $wp_registered_sidebars;
$i = count( $wp_registered_sidebars ) + 1;
$id_is_empty = empty( $args['id'] );
$defaults = array(
/* translators: %d: Sidebar number. */
'name' => sprintf( __( 'Sidebar %d' ), $i ),
'id' => "sidebar-$i",
'description' => '',
'class' => '',
'before_widget' => '<li id="%1$s" class="widget %2$s">',
'after_widget' => "</li>n",
'before_title' => '<h2 class="widgettitle">',
'after_title' => "</h2>n",
'before_sidebar' => '',
'after_sidebar' => '',
'show_in_rest' => false,
);
/**
* Filters the sidebar default arguments.
*
* @since 5.3.0
*
* @see register_sidebar()
*
* @param array $defaults The default sidebar arguments.
*/
$sidebar = wp_parse_args( $args, apply_filters( 'register_sidebar_defaults', $defaults ) );
if ( $id_is_empty ) {
_doing_it_wrong(
__FUNCTION__,
sprintf(
/* translators: 1: The 'id' argument, 2: Sidebar name, 3: Recommended 'id' value. */
__( 'No %1$s was set in the arguments array for the "%2$s" sidebar. Defaulting to "%3$s". Manually set the %1$s to "%3$s" to silence this notice and keep existing sidebar content.' ),
'<code>id</code>',
$sidebar['name'],
$sidebar['id']
),
'4.2.0'
);
}
$wp_registered_sidebars[ $sidebar['id'] ] = $sidebar;
add_theme_support( 'widgets' );
/**
* Fires once a sidebar has been registered.
*
* @since 3.0.0
*
* @param array $sidebar Parsed arguments for the registered sidebar.
*/
do_action( 'register_sidebar', $sidebar );
return $sidebar['id'];
}
Hooks
- do_action( ‘register_sidebar’, array $sidebar )
-
Fires once a sidebar has been registered.
- apply_filters( ‘register_sidebar_defaults’, array $defaults )
-
Filters the sidebar default arguments.
Skip to note 7 content
Codex
This example creates a sidebar named “Main Sidebar” with and before and after the title.
/** * Add a sidebar. */ function wpdocs_theme_slug_widgets_init() { register_sidebar( array( 'name' => __( 'Main Sidebar', 'textdomain' ), 'id' => 'sidebar-1', 'description' => __( 'Widgets in this area will be shown on all posts and pages.', 'textdomain' ), 'before_widget' => '<li id="%1$s" class="widget %2$s">', 'after_widget' => '</li>', 'before_title' => '<h2 class="widgettitle">', 'after_title' => '</h2>', ) ); } add_action( 'widgets_init', 'wpdocs_theme_slug_widgets_init' );Skip to note 8 content
tazotodua
To output registered sidebar:
<ul id="sidebar"> </ul>Skip to note 9 content
Stefan Budimirovic
/* Better way to add multiple widgets areas */ function widget_registration($name, $id, $description,$beforeWidget, $afterWidget, $beforeTitle, $afterTitle){ register_sidebar( array( 'name' => $name, 'id' => $id, 'description' => $description, 'before_widget' => $beforeWidget, 'after_widget' => $afterWidget, 'before_title' => $beforeTitle, 'after_title' => $afterTitle, )); } function multiple_widget_init(){ widget_registration('Footer widget 1', 'footer-sidebar-1', 'test', '', '', '', ''); widget_registration('Footer widget 2', 'footer-sidebar-2', 'test', '', '', '', ''); // ETC... } add_action('widgets_init', 'multiple_widget_init');Skip to note 10 content
jave.web
Sidebar id must be in lowercase ! (and no spaces)
From codex: 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.
Skip to note 11 content
Vanoisbe
/* Add Multiple sidebar */ if ( function_exists('register_sidebar') ) { $sidebar1 = array( 'before_widget' => '<div class="widget %2$s">', 'after_widget' => '</div>', 'before_title' => '<h2 class="widgettitle">', 'after_title' => '</h2>', 'name'=>__( 'My sidebar 1', 'textdomain' ), ); $sidebar2 = array( 'before_widget' => '<div class="widget %2$s">', 'after_widget' => '</div>', 'before_title' => '<h2 class="widgettitle">', 'after_title' => '</h2>', 'name'=>__( 'My sidebar 2', 'textdomain' ), ); $sidebar3 = array( 'before_widget' => '<div class="widget %2$s">', 'after_widget' => '</div>', 'before_title' => '<h2 class="widgettitle">', 'after_title' => '</h2>', 'name'=>__( 'My sidebar 3', 'textdomain' ), ); $sidebar4 = array( 'before_widget' => '<div class="widget %2$s">', 'after_widget' => '</div>', 'before_title' => '<h2 class="widgettitle">', 'after_title' => '</h2>', 'name'=>__( 'My sidebar 4', 'textdomain' ), ); register_sidebar($sidebar1); register_sidebar($sidebar2); register_sidebar($sidebar3); register_sidebar($sidebar4); }register_sidebarsinstead.Skip to note 12 content
gabrielsgsm
Note: when calling
is_sidebar_active()ordynamic_sidebar(), the argument passed in these function is always sanitized viasanitize_title(). This function, amongst the other transforms, also lowercases the$indexthat is being passed.When using those two functions, you should remember to register the sidebar name in all lowercase and with a
sanitize_title()compatible id, otherwise calling the widget area will always return false.