函数原型
register_sidebar( array|string $args = array() ): string
函数描述
Builds the definition for a single sidebar and returns the ID.
是否弃用
未弃用
函数参数
-
$args
array|string
Optional - 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.
Default empty string.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.
Default empty string.after_sidebarstringHTML content to append to the sidebar when displayed.
Outputs before the 'dynamic_sidebar_after' action.
Default empty string.show_in_restboolWhether to show this sidebar publicly in the REST API.
Defaults to only showing the sidebar to administrator users.
Default:
array()
函数返回值
string Sidebar ID added to $wp_registered_sidebars global.
函数位置
File: wp-includes/widgets.php.
函数源码
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'];
}
源码链接
变更日志
| Version | Description |
|---|---|
| 5.9.0 | Added the show_in_rest argument. |
| 5.6.0 | Added the before_sidebar and after_sidebar arguments. |
| 2.2.0 | Introduced. |

