• Hello,

    I triy to add a ‘logout’ button on the main nav menu under ‘account’ parent menu. But, the button does not rendor.

    1. At Appearance > Menus, I add ‘account’ page to the active menu. (link enabled)
    2. I add a code snippet

    The code here;

    function my_insert_primary_nav_login_link( $items, $menu, $args ) {	
    if( is_admin() )
    return $items;

    // Bail out if this menu isn't
    main-menu or Main Menu is being loaded in the dashboard.
    if( $menu->slug !== 'main-menu' || is_admin() )
    return $items;

    // Find the ID of the Page with the slug account in the menu items.
    foreach( $items as $item ) {
    if( $item->post_type !== 'page' || $item->post_name !== 'account' )
    continue;

    $account_page_id = $item->ID;
    break;
    }

    // Bail if the account page is not in this menu.
    if( ! isset( $account_page_id ) )
    return $items;

    $logged_in = is_user_logged_in();
    $link_title = __( $logged_in ? 'Log In' : 'Log Out', 'oceanwp' );

    // Insert a new WP_Post object for the link.
    $items[] = new WP_Post(
    (object) [
    'ID' => 'user-login',
    'menu_item_parent' => $account_page_id,
    'classes' => [ 'account-auth-link' ],
    'type' => 'virtual',
    'object' => 'virtual',
    'title' => $link_title,
    'attr_title' => $link_title,
    'url' => $logged_in ? wp_logout_url() : wp_login_url(),
    ]
    );

    return $items;
    }

    add_filter( 'wp_get_nav_menu_items', 'my_insert_primary_nav_login_link', 10, 3 );

    If you could review the code, I would really appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello @cgray24,

    Thank you for reaching out,

    Unfortunately, reviewing or assisting with custom code is outside the scope of our support.

    However, we do have a shortcode available that you can use to achieve what you need.
    You can find more information here: https://docs.oceanwp.org/article/373-oceanwplogin-shortcode.

    Let us know if you have any other questions.

    Hope it helps,
    Best Regards

    Thread Starter cgray24

    (@cgray24)

    Dear Shahin,

    Understood. Then, can you confirm the primary menu slug is main-menu? In your theme function.php seemingly modify the default slug ‘primary.’

    Hello @cgray24,

    Thank you for reaching out,

    From the path WP Dashboard > Appearance > Menus, you create the menu slugs yourself.
    and you’ll find the primary location in the Manage Locations tab: https://ibb.co/d4jcM61K

    I hope it helps,
    Best Regards

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.