This will filter the navigation block in Twenty Twenty Four.
add_filter( 'render_block_core/navigation', 'wpsites_nav_menu_admins', 10, 2 );
function wpsites_nav_menu_admins( $block_content, $block ) {
if ( isset( $block['blockName'] ) && 'core/navigation' === $block['blockName'] ) {
if ( is_user_logged_in() && current_user_can( 'administrator' ) ) {
$custom_message = '<p class="admin-message">Welcome, Admin!</p>';
$block_content = $custom_message . $block_content;
}
}
return $block_content;
}
To target a specific menu item., use code like this :
add_filter( 'render_block_core/navigation', 'wpsites_modify_specific_menu_item_for_admins', 10, 2 );
function wpsites_modify_specific_menu_item_for_admins( $block_content, $block ) {
if ( isset( $block['blockName'] ) && 'core/navigation' === $block['blockName'] ) {
if ( is_user_logged_in() && current_user_can( 'administrator' ) ) {
$dom = new DOMDocument();
@$dom->loadHTML('<?xml encoding="utf-8" ?>' . $block_content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXPath($dom);
$menu_items = $xpath->query('//a');
foreach ($menu_items as $menu_item) {
if ($menu_item->textContent === 'Product') {
$menu_item->setAttribute('class', 'admin-custom-class');
// $menu_item->setAttribute('href', 'https://example.com/admin-special');
}
}
$block_content = $dom->saveHTML();
}
}
return $block_content;
}