Log In/Out Link at the End of the Navigation Block
Adds a Log in / Log out link as the last item of your block theme's header navigation automatically, using block hooks.
Opens WPIDE on
Code
add_filter( 'hooked_block_types', function ( $hooked, $position, $anchor, $context ) {
if ( 'last_child' !== $position || 'core/navigation' !== $anchor ) {
return $hooked;
}
if ( $context instanceof WP_Block_Template && 'header' !== $context->area ) {
return $hooked;
}
$hooked[] = 'core/loginout';
return $hooked;
}, 10, 4 );
Notes
Applies to Navigation blocks inside the header template part. Remove it in the Site Editor if you change your mind. Needs WordPress 6.5+.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.