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.
Joe is an experienced full-stack web developer with a decade of industry experience in the LAMP & MERN stacks, WordPress, WooCommerce, and JavaScript – (diverse portfolio). He has a passion for creating elegant and user-friendly solutions and thrives in collaborative environments. In his spare time, he enjoys exploring new tech trends, tinkering with new tools, and contributing to open-source projects. You can hire me here for your next project.
Similar Articles
- How to Access WordPress Admin Dashboard or log in to your WordPress Dashboard
- How to Redirect to Cart after Login in WooCommerce
- How to Hide Prices From Google In WooCommerce
- How to Hide Tax Label In WooCommerce
- How to Hide Shipping Methods for Certain Conditions
- How to Set Up WooCommerce You Must be Logged in to Checkout
- How to Add WooCommerce Invoice Checkout Page
- How to Hide WooCommerce Marketing Hub Admin Option
- How to Create WooCommerce Secret Product
- How to Hide Quantity In Stock In WooCommerce
- How to Redirect Users after Successful Login in WordPress without Using a Plugin
- WooCommerce Mobile Checkout Optimization Quick Guide
- How to Force Secure Checkout In WooCommerce
- 30 Best WordPress Security Plugins to Protect Your Site
- How to Change WooCommerce Checkout Page Title
- How to Hide the Weight of Product in WooCommerce
- WooCommerce Difference Between PayPal and PayPal Checkout
- How to Hide Errors From Visitors WooCommerce