I’m adding login/logout links to a nav menu using the following code:
function rp_add_login_logout_links( $items, $args ) {
$link = wp_loginout( get_permalink( get_option( 'woocommerce_myaccount_page_id' ) ), false );
if ( 'Shop Menu' == $args->menu ) {
$items .= '<li id="menu-item-login" class="menu-item menu-item-login">' . $link . '</li>';
}
return $items;
}
add_filter( 'wp_nav_menu_items', 'rp_add_login_logout_links', 10, 2 );
Since $items
is a string, I can really only easily prepend or append the links to the list. I need this link to appear before a certain menu item in the list, it would end up being the third item from the end. Is there another filter or method I could use to accomplish this?
I tried using strpos
to find the nav item I was looking for and manipulate the $items
string but couldn’t get it to match the menu item HTML.
Turns out there is a
wp_nav_menu_objects
filter that allows you to modify the array of nav menu items before they are joined into a string. I was able to accomplish what I needed using the following function: