How to Create WooCommerce Login Logout Shortcode

woocommerce login logout shortcode

Do you want to create a WooCommerce Login Logout Shortcode that you can add to any page on your WooCommerce store to give your customers the ability to log in or log in from any place?

In this quick post, I want to show you how you can quickly and easily add a shortcode that has the login and logout functions to your store.

First, it helps to check this post here I shared how to create a WooCommerce logout page shortcode.

If you also want to learn how you create a custom WooCommerce redirect after logout, this complete guide on how to create a WooCommerce logout redirect can be a good place to start.

WooCommerce Login Logout Shortcode

To create the WooCommerce login logout shortcode you should add the following code to your theme functions.php file and use the shortcode anywhere and in the templates using the do_shortcode() function.

add_shortcode( 'njengah_login_logout', 'njengah_login_logout_shortcode_callback' );
/**
* Add a login/logout shortcode button
*/
function njengah_login_logout_shortcode_callback() {
ob_start();
if (is_user_logged_in()) :
// Set the logout URL - below it is set to the root URL
?>
<a role="button" href="<?php echo wp_logout_url('/'); ?>">Log Out</a>

<?php
else :
// Set the login URL - below it is set to get_permalink() - you can set that to whatever URL eg '/whatever'
?>
<a role="button" href="<?php echo wp_login_url(get_permalink()); ?>">Log In</span></a>

<?php
endif;

return ob_get_clean();
}

You can now use the shortcode [njengah_login_logout] to display the WooCommerce Login Logout Shortcode anywhere on the post or page as I showed on this post  – how to use the shortcode on the page or post.

Conclusion

In this post, we have seen how you can create a shortcode to add a login and logout button to your WooCommerce store. You can change this shortcode [njengah_login_logout] to your preferred shortcode in the code and use it accordingly in your posts, pages, or templates.

Similar Articles

  1. How to Hide Errors From Visitors WooCommerce