Skip Navigation

[Resolved] Creating a logout link for menu item

This support ticket is created 3 years 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
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9: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: Africa/Casablanca (GMT+01:00)

This topic contains 2 replies, has 2 voices.

Last updated by Alok Sharma 3 years ago.

Assisted by: Jamal.

Author
Posts
#2235287

Hi,

I want to add a logout link in the menu item.

If I add [wpv-logout-link] in the URL field, it doesn't work. And if I add /wp-login.php?action=logout&redirect_to=/my-account/ in the URL field, it does work, but it asks for the confirmation.

Is there anyway to create a logout link that logs out the user directly, without asking for their confirmation.

Thanks.

#2235365

Hello and thank you for contacting the Toolset support.

By default, WordPress will not evaluate shortcodes inside the menus. Toolset does not actually have any effects on menus. However, you can run a shortcode inside a menu using a 3rd party plugin such as https://wordpress.org/plugins/shortcode-in-menus/

You may also want to use a specialize plugin such as https://wordpress.org/plugins/login-logout-menu/

I hope this helps. Let me know if you have any questions.

#2236789

Hi Jamal,

For some reasons, the plugin didn't work for me. I did some research and came across a solution over here hidden link

/**
* Generates custom logout URL
*/
function getLogoutUrl($redirectUrl = ''){
if(!$redirectUrl) $redirectUrl = site_url();
$return = str_replace("&", '&', wp_logout_url($redirectUrl));
return $return;
}

/**
* Bypass logout confirmation on nonce verification failure
*/
function logout_without_confirmation($action, $result){
if(!$result && ($action == 'log-out')){
wp_safe_redirect(getLogoutUrl());
exit();
}
}
add_action( 'check_admin_referer', 'logout_without_confirmation', 1, 2);

I tried it out and it works.

First, if WP can't find a valid wpnounce parameter & value in the logout URL, it will display the logout confirmation page. The above code adds the necessary wpnounce parameter & value on the fly.

The next part is adding the logout URL in the menu item or wherever. The logout URL should be:

"/wp-login.php?action=logout"

That's it.

This code can be added in theme's function.php file or in a plugin like Code Snippets.