How to create a WooCommerce customer login shortcode, page and widget

In this article, we’ll show you how to create a WooCommerce customer login shortcode. Some technical aspects are covered, as well as how they can be applied to your site. In addition, we’ll look into error handling, to make sure it doesn’t break your site in case WooCommerce is disabled.

Easy login forms are great to improve your site usability. If you have many login options, you make sure your users won’t be lost on your site. This allows them to quickly place new orders, as well as track old ones.

WooCommerce’s original solution for this is using a login form in the user profile and some other pages. But this still doesn’t have the flexibility aspect that we need. That’s why we are going to create our login form shortcode.

Thus, this shortcode can be used on any of your pages. Additionally, it can be used in widget areas or even in your template files. Hence, one of the main aspects of our shortcode is to gracefully handle errors. Then we need to check for WooCommerce disabling and error messages.

Let’s get started!

woocommerce customer login

Different types of WooCommerce login setups

There are a few different ways a WooCommerce store can be structured in relation to its customer’s login requirements.

  • The default: A public WooCommerce store is the default and the most common setup. Anyone can browse your products and there is no hidden WooCommerce content. Customers can buy products as a guest without having to log in, or they can optionally sign up and create an account during the guest checkout.
  • Public WooCommerce store with required user registration. This is similar to the default setup in the way that all the products in the store are public and can be browsed by anyone, but customers must register (by company staff or via a dedicated WooCommerce registration page) or login to their account page to purchase. They can log in via their custom WooCommerce login page or by using a WooCommerce social login plugin.
  • Private and protected WooCommerce products and product categories. With this kind of setup, customers can view your products and make purchases on your WooCommerce site, but only customer accounts that have been created for the purposes of viewing specific products have access to certain things. For example, all customers can purchase from any of the public areas of your website which don’t contain hidden content. Only customer accounts with specific roles and privileges can view and purchase certain products.
  • WooCommerce customer portal. A WooCommerce client portal is a private, secure WooCommerce customer area on your website where you can share and exchange confidential information with your clients. It provides a set of tools that help your clients to share information with you, and each other and communicates efficiently. The WooCommerce customer portal usually has password-protected product categories. It can be valuable as a self-service tool in a private customer area. This allows specific users or user roles to access certain areas of the WooCommerce store. Customers can view and purchase products from the public areas of the shop, while only those with the correct credentials can access the protected categories.

Default WooCommerce customer login options

WooCommerce has some out-of-the-box options for user login. Usually, the login form is presented whenever you visit a WooCommerce page for registered users.

The first instance is the “My Account” page, which is usually under /my-account. On this page, you can see the login form and the registration form.

The checkout page may contain the login form as well, depending on your setup. The main difference here is the inline opening form. Therefore, once you click “log in”, the form opens up as an accordion. That’s a great solution to save space, but also keep users on the checkout workflow.

If you want the login form by itself, though, WooCommerce has no option for it. Now let’s build our shortcode to change this.

By default anyone can browse your products and there is no hidden WooCommerce content.

How to customize the user login in WooCommerce

There are a few different ways that you can use to customize your WooCommerce login page. The first and the most obvious one is to use a dedicated WordPress theme that provides an out-of-the-box custom login page. The other alternative is to customize your login page by using a dedicated user registration WordPress plugin. To create a customer login page, you’ll need to install and configure a WordPress plugin like the User Registration plugin. You can then use this plugin to create a customer login form or even a customer registration form if they haven’t logged in before. Since both of these ways are quite straightforward in this tutorial we are going to focus on how to create a custom WooCommerce login form and shortcode programmatically with some custom code.

Unlock the Full Potential of Your WooCommerce Customer Data

How to create a WooCommerce login shortcode

The first step in our shortcode is to register the shortcode in the WordPress add_shortcode() function. This is its main structure, along with the main points in our code:

The name is what you use in your square brackets – in this case, the code registers a shortcode that can be used as [my_wc_login_form]. And the function name is what WordPress calls to render the shortcode content.

By this point, the code will not do anything. Now let’s start implementing the above-listed steps one by one.

1. Check if the user is logged in

We want to check if users are logged in, and if they are – we’ll display a welcome message. If users aren’t logged in, then we do our magic to display the form.

You can also check if a user is logged in with Users Insights. You will need to just apply the last seen filter and set the days field to less than one:

woocommerce login user

The green dot on the left side from the user profile picture is indicated in that the WooCommerce user is currently online.

2. Display login form

For the non-logged-in users, we can display our login form. But we need to know if the WooCommerce functions that render the form are available. We can do this by using the function_exists() function.

This code renders the login form:

  • the woocommerce_login_form() the function prints the form markup. The woocommerce_output_all_notices() function will print any login errors, such as invalid username/password errors.
  • we use ob_start() and ob_get_clean() since the WooCommerce functions echo the markup, and the shortcode is supposed to return the markup instead of printing it.
  • if the WooCommerce login functions are not present, we render the default WordPress login with the wp_login_form() function

Important: please keep in mind that the WooCommerce functions that we used here might change in the future. Therefore it is always highly recommended to always test your code after updating your WooCommerce plugin.

And this is the final version of our code:

At this point, you might be asking yourself – why use the WooCommerce login form functions, instead of directly using the WordPress login form function? There are a few advantages of using the WooCommerce form, such as:

  • consistent input element style – since the form uses the default WooCommerce classes for its elements, it will look the same way as the other WooCommerce login forms on your site
  • better error handling – if the authentication fails, the WooCommerce form will display a message on the same page, while the WordPress form will redirect your users to the default WordPress login page

Now let’s use the login shortcode on your site.

How to use the WooCommerce login shortcode

You can apply this shortcode in almost any place you want. But let’s add it to one of our pages. For example, if you have a login page, you can create it this way:
Add WooCommerce login shortcode

And this is the result:
WooCommerce login form

On the other hand, you can use this shortcode in your sidebars. This is my personal favorite, as it fits quite nicely in the sidebar area. This is how you create it in your widget areas:
Add WooCommerce login shortcode to sidebar

And this is the result we have:
WooCommerce login shortcode in sidebar

Furthermore, you can use your WooCommerce login shortcode in your template files or inside of other shortcodes. For other shortcodes, this depends on your current plugins. Usually, sliders, columns, accordions, and other elements allow shortcodes in them. For a template edit, you can apply it with this code:


Today we looked into how to create your WooCommerce login shortcode. You can use his shortcode anywhere you want. This makes it powerful for a solution in a page, widget, slider, or even in other shortcodes.

We hope you enjoyed this article and see you again next time!