register_sidebar()
Проверено на WordPress 6.9, обновлено Источник: WordPress Developer Resources.
Сигнатура
register_sidebar( array|string $args = array() ): string
Описание
Принимает строку или массив и затем разбирает их относительно набора аргументов по умолчанию для новой боковой панели. Если эти аргументы не указаны, WordPress автоматически сформирует ID и имя боковой панели на основе текущего числа зарегистрированных боковых панелей.
При автоматическом формировании параметров name и ID учитывайте, что счётчик боковой панели со временем может меняться в зависимости от того, какие ещё плагины и темы установлены.
Если на момент вызова функции поддержка ‘widgets’ ещё не была добавлена в тему, она будет включена автоматически с помощью add_theme_support() .
Оригинал (английский)
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() .
Параметры
$args
array|string
необязательный
= array()
Возвращаемое значение
string
Исходный код
wp-includes/widgets.php:269
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'];
}
История изменений
| Версия | Описание |
|---|---|
| 5.9.0 | Added the show_in_rest argument. |
| 5.6.0 | Added the before_sidebar and after_sidebar arguments. |
| 2.2.0 | Introduced. |

