Skip Navigation

[Resolved] Logout link in menu

This support ticket is created 7 years, 7 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.

Our next available supporter will start replying to tickets in about 0.86 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 14 replies, has 3 voices.

Last updated by Team DNK 7 years, 7 months ago.

Assisted by: Noman.

Author
Posts
#488557

Hello,
I want to create a logout link into a menu ; how may I set this up ?
Thank you.

#488563

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Roman,

Thank you for contacting Toolset Support.

Please add the following code to your theme’s functions.php file.

Go to Appearance > Editor > Theme Functions (functions.php). Place the following code in the bottom of your functions file and press “Update File.”

add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2); function add_login_logout_link($items, $args) {         ob_start();         wp_loginout('index.php');         $loginoutlink = ob_get_contents();         ob_end_clean();         $items .= '<li>'. $loginoutlink .'</li>';     return $items; }

Have a great day.
Thank you

#488575

Hello and thank you very much,
The code works great but there are still a few problems :
1- Since I use a custom layout for the login form, I wonder if it's possible to redirect "connexion" to the specific page ;
2- I would also like to have a different text : for instance "client access" instead of "connexion" ;
3- Last, the code added the link to all of my menus, but I need it only on one of them.
Thank you.

#488630

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello Roman,

2. I would also like to have a different text : for instance "client access" instead of "connexion"
>> Please send me screenshot and link, where you want to replace this text?

For Point 1 and 3.
>> Please add the following code to your theme’s functions.php file.

//Add login/logout link to naviagation menu
function add_login_out_item_to_menu( $items, $args ){

	//change theme location with your theme location name
	if( is_admin() ||  $args->theme_location != 'primary' )
		return $items; 

	$redirect = ( is_home() ) ? false : get_permalink();
	if( is_user_logged_in( ) )
		$link = '<a href="' . wp_logout_url( $redirect ) . '" title="' .  __( 'Logout' ) .'">' . __( 'Logout' ) . '</a>';
	else  $link = '<a href="' . wp_login_url( $redirect  ) . '" title="' .  __( 'Login' ) .'">' . __( '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-out:
add_action(
  'wp_logout',
  create_function(
    '',
    'wp_redirect("'.get_permalink(234).'");exit();'
  )
);

Please note that you should:
- On line #5, Replace primary with name of your theme’s menu location (primary, footer, top bar, etc…).

- On line #21, Replace 234 with your Page ID.

Thank you

#488691

Hello and thank you,

I have tried to replace "primary" with "top", "top bar", "top-bar", "top_bar", "top-menu", "Top", "Top Position"... with no effect... How may I find the menu name ? In backend it says "Top", and in front end the css ul id is "#menu-top"...

Also, I see I can now choose a "after logout" redirection, but I can't find where to set the "after login" redirection ?

Thank you.

#488920

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

menu location.png
redirect after logout.png

Hello Roman,

To display Logout/Login link in menu, first you need to select that menu, please go to: Appearance >> Menu >> Menu Settings >> Display Location (see attached screenshot menu location.png)

Replace 234 with the Page ID where you want to redirect user after Logout (See attached screenshotredirect after logout.png)

Thank you

#488938
menus.JPG

Hello and thank you,

As you may see, the top bar menu (where I want the button) is called "top".
But nothing appears...

My other question was on after logIN redirection : when a user logs in, he should be redirected directly to a specific page.

Thank you.

#488944

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

9898.png

Hello Roman,

1. To redirect user after login, please add the following code to your theme’s functions.php file.

//Redirect to Specific Page after user is Logged-in:
add_action(
  'wp_login',
  create_function(
    '',
    'wp_redirect("'.get_permalink(76).'");exit();'
  )
);

- Like before, Replace 76 with the Page ID where you want to redirect user after Login.

2. From the screenshot I can see the actual menu name is "Top Navigation". To get the actual name please inspect that text next to the checkbox, see attached screenshot for reference. Anything that is after " locations- " is the menu location's name that needs to be replaced in the code.

If you can't find it after trying above then I will need login to your site to see why its not working.
Thank you

#488950

It was in deed "top_navigation" ! Thank you !!

Everything works great now. One last question : I use my own page for login ; how could I make the "login" button click redirect to this page instead of wordpress standard login page ?

Thank you !

#488991

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello Roman,

It depends on how you have setup your Custom Login Page, generally you can achieve this using the following method.

- Please add this code to your theme’s functions.php file.
- Replace 390 with the Page ID where you want to redirect user when clicking on Login button or link:

// Redirect user to Custom Login Page
function redirect_login_page(){

    // Store for checking if this page equals wp-login.php
    $page_viewed = basename( $_SERVER['REQUEST_URI'] );

    // permalink to the custom login page
    $login_page  = get_permalink( '390' );

    if( $page_viewed == "wp-login.php" ) {
        wp_redirect( $login_page );
        exit();
    }
}

add_action( 'init','redirect_login_page' );

- After adding the code please close the tab, clear browser cache and re-open the page. Please let us know if it works good for you as well.

Thank you

#489093

Hello and thank you,
It didn't work but I managed by replacing a line of the first code in a hard URL :

  if( is_user_logged_in( ) )
        $link = '<a href="' . wp_logout_url( $redirect ) . '" title="' .  __( 'Déconnexion' ) .'"><i class="fa glyphicon fa-lock"></i>' . __( 'Déconnexion' ) . '</a>';
    else  $link = '<a href="<em><u>hidden link</u></em>" title="' .  __( 'Accès Client' ) .'"><i class="fa glyphicon fa-lock"></i>' . __( 'Accès Client' ) . '</a>';

Thank you very much for your help !

#489191

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Great to hear that you were able to make it work 🙂 We are happy to help you and thank you for sending the modified code as well. It will help our community in general.

Have a great day,
Thank you

#493547

Hello Norman... Hoping you can give me a hand as well. I am wanting to do the the exact same thing but I am not able to get the Login/Logout link to appear in the menu. 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

#494462

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Dave,

Thank you for reaching out to us. This ticket is closed, please submit your new ticket in the Professional Support Forum so we can better handle it:
https://toolset.com/forums/forum/support-2/

Thank you

#494624

OK Understood... New ticket can be found at the below link.

https://toolset.com/forums/topic/dynamic-loginlogout-link-with-cred-login-form-and-redirects/

Kind Regards,
Dave

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