Добавление nav_menu в admin_bar (toolbar)
Этот пункт может быть практически незаменим, потому что кроме удобства он еще и добавляет функциональности.
Использовав следующий код мы регистрируем меню навигации wp_nav_menu и добавляем пункты из этого меню в панель инструментов.
Я написал этот код как замена своему уже устарелому плагину:
/**
* toolbar nav menu - навигационное меню в тулбаре
* v0.3
*/
add_action( 'after_setup_theme', function() {
register_nav_menu( 'toolbar', 'Панель инструментов' );
} );
add_action( 'admin_bar_menu', 'kama_add_toolbar_menu', 999 );
function kama_add_toolbar_menu( $toolbar ) {
$locations = get_nav_menu_locations();
if( ! isset( $locations['toolbar'] ) ){
return;
}
$items = wp_get_nav_menu_items( $locations['toolbar'] );
if( ! $items ){
return;
}
foreach( $items as $item ){
$args = [
'parent' => $item->menu_item_parent ? 'id_' . $item->menu_item_parent : false,
'id' => 'id_' . $item->ID,
'title' => $item->title,
'href' => $item->url,
'meta' => [
// 'html' - The html used for the node.
// 'class' - The class attribute for the list item containing the link or text node.
// 'rel' - The rel attribute.
// 'onclick' - The onclick attribute for the link. This will only be set if the 'href' argument is present.
// 'target' - The target attribute for the link. This will only be set if the 'href' argument is present.
// 'title' - The title attribute. Will be set to the link or to a div containing a text node.
// 'tabindex' - The tabindex attribute. Will be set to the link or to a div containing a text node.
'class' => implode( ' ', $item->classes ),
'title' => esc_attr( $item->description ),
'target' => $item->target,
],
];
$toolbar->add_node( $args );
}
}
Как это работает.
Добавляете код в functions.php. Затем заходите в админку: Внешний вид > Меню и создаете меню, добавляете в него любые ссылки и прикрепляете меню к области «Панель инструментов». Можно использовать несколько уровней, тогда верхний уровень будет главным в панели инструментов, а дополнительные станут выпадающим списком.
—
Заметка встроена в: 11 хаков для Админ-бара WordPress (тулбара)
