Join

WooCommerce Customer Login Shortcode

Don't want to mess with code snippets? Request for this to be a feature of MyListing Pro.

Instructions

  1. Create a new PHP code snippet.
  2. Copy the contents of code snippet below.
  3. Paste the contents into your code snippet.
  4. Review any notes that I’ve provided.
  5. Save and enable the code snippet.
  6. Test.

Snippet

This code snippet is useful for situations like creating separate Login and Registration pages.

<?php

add_shortcode("wc_login_form_wptu", "wptu_separate_login_form");

function wptu_separate_login_form()
{
    if (is_user_logged_in()) {
        return "<p>You are already logged in</p>";
    }
    ob_start();
    do_action("woocommerce_before_customer_login_form");
    woocommerce_login_form(["redirect" => wc_get_page_permalink("myaccount")]);
    return ob_get_clean();
}