Skip Navigation

[Resolved] Dynamic Login/Logout Link With CRED Login Form And Redirects

This support ticket is created 7 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

Author
Posts
#493787

Hello Support... I am starting a new thread as the thread I originally posted this inquiry on is closed and have not received a response.
https://toolset.com/forums/topic/logout-link-in-menu/

I am wanting to place a dynamic Login/Logout link in my main menu. When user is logged out the Login link would take them to a custom CRED Login page for login and the form would redirect the user after login, so not sure if I need the redirection code used below. When the user logs out they should be redirected to a specified page.

Have gone over the steps from the other post many times as well as quite a bit of research on Google. Not able to get the link to show up. I have provide my latest iteration of the code I am putting at the end of my functions.php file. Would value your assistance. I am using the Toolset Starter Theme and it, as well as all Toolset plugins, are up to date as of this post.

//Add login/logout link to naviagation menu
function add_login_out_item_to_menu( $items, $args ){
  
    if( is_admin() ||  $args->theme_location != 'header-menu' )
        return $items; 
  
    $redirect = ( is_home() ) ? false : get_permalink();
     
    if( is_user_logged_in( ) )
    $link = '<a href="' . wp_logout_url( $redirect ) . '" title="' .  __( 'Logout' ) .'"><i class="fa glyphicon fa-lock"></i>' . __( 'logout' ) . '</a>';
    else $link = '<a href="<em><u>hidden link</u></em>" title="' .  __( 'Login' ) .'"><i class="fa glyphicon fa-lock"></i>' . __( 'login' ) . '</a>';
  
    return $items.= '<li id="log-in-out-link" class="menu-item menu-type-link">'. $link . '</li>';
}
 
add_filter( 'wp_nav_menu_items', 'add_login_out_item_to_menu', 50, 2 );
 
 
//Redirect to Specific Page after user is Logged-in:
add_action(
  'wp_login',
  create_function(
    '',
    'wp_redirect("'.get_permalink(36).'");exit();'
  )
);
 
 
//Redirect to Specific Page after user is Logged-out:
add_action(
  'wp_logout',
  create_function(
    '',
    'wp_redirect("'.get_permalink(2).'");exit();'
  )
);

Kind Regards,
Dave

#493843

ADDTIONAL INFO:
Still working on this and after reading other support threads I thought I should clarify once again that I am using the Toolset Starter Theme and my main menu is added to the header using the latest building methods via Toolset Layouts. Not sure if this adds a level of complexity that the code my not be taking into consideration. Again would value your assistance.

#494269

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Could you pleases try to follow the following code to your themes functions.php file:

add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 );
function add_loginout_link( $items, $args ) {
    if (is_user_logged_in() && $args->theme_location == 'primary') {
        $items .= '<li><a href="'. wp_logout_url() .'">Log Out</a></li>';
    }
    elseif (!is_user_logged_in() && $args->theme_location == 'primary') {
        $items .= '<li><a href="'. site_url('wp-login.php') .'">Log In</a></li>';
    }
    return $items;
}
#494725

Hi Minesh... Gave the provided code a try and it does not appear to work. Tried it as is as well as replaced "primary" with the Toolset Starter Theme "header-menu". Please advise?

Kind Regards,
Dave

#494941

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I just got info that menu with Toolset starter theme is actually using layouts cell.

What if you try to use the following plugin that will quickly resolve your issue:
=> hidden link

#495165

Hello Minesh... Thank you for confirming that this method does not in fact work with the Toolset Starter Theme. As for the plugin I have used that and yes it is quite simple and affective, just not the amount of flexibility I was looking for.

During all this I did come to another approach on my own that happens to be totally a Toolset centric approach. Here is a link to the thread with my solution. It is fairly complete but there were a couple typos that I hope are not confusing to others. The edit functionality timed out before I got a chance to update it.

https://toolset.com/forums/topic/how-can-i-build-a-toolset-style-loginlogout-menu/

I really wish Toolset had someone who could approach your product from this perspective and provide purpose centric solutions that would help users like myself who are coming from the professional business side of things not the development site building services side. Toolset is a powerful business tool, I hope it continues to grow as such.

Thanks for all you do,
Dave

This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.