Typek – Blog

How to Remove the Register Form in WooCommerce Using PHP Snippet

WooCommerce is a highly customizable e-commerce platform that allows you to create online stores with WordPress. One of the many features it provides is the ability for users to create accounts on your site. However, there may be instances where you do not want to allow users to register on your site, and you would like to remove the registration form altogether. In this tutorial, we will discuss how to remove the register form in WooCommerce using a PHP snippet.

Overview of the Register Form in WooCommerce

When you install and activate WooCommerce on your WordPress site, it automatically adds a registration form to the “My Account” page. This form allows users to create an account on your site, which can be useful for tracking their orders and managing their personal information.

However, there may be cases where you want to disable the registration form for various reasons, such as:

  • You want to restrict the access to your online store to a specific group of users
  • You are using a third-party service for managing user accounts
  • You want to simplify the checkout process and avoid having users create an account

In these cases, you can remove the register form in WooCommerce using a PHP snippet that we will cover in the next section.

Using a PHP Snippet to Remove the Register Form

To remove the register form in WooCommerce, you can use the following PHP snippet:


add_action('init', 'typek_remove_register_form');

function typek_remove_register_form() {
    if (class_exists('WooCommerce')) {
        remove_action('woocommerce_before_customer_login_form', 'woocommerce_output_registration_form_wrap');
        remove_action('woocommerce_after_customer_login_form', 'woocommerce_output_registration_form_wrap');
    }
}

This snippet uses the add_action() function to hook our custom function typek_remove_register_form() to the init action. The init action is called after WordPress has finished loading but before any headers are sent, making it an ideal hook for our purpose.

Inside the typek_remove_register_form() function, we first check if the WooCommerce class exists using the class_exists() function. This ensures that our code only runs if WooCommerce is installed and active on our site.

If WooCommerce is active, we use the remove_action() function to remove the registration form output from the woocommerce_before_customer_login_form and woocommerce_after_customer_login_form actions. These actions are responsible for displaying the registration form on the “My Account” page.

Creating a Child Theme to Implement the PHP Snippet

Before we implement the PHP snippet, it is important to create a child theme in your WordPress site. A child theme allows you to make changes to your site without modifying the original theme files. This ensures that your changes are not lost when the parent theme is updated.

To create a child theme, follow these steps:

  1. Create a new directory in your wp-content/themes directory. You can name it anything you like, but it is a good practice to use the parent theme name followed by “-child”. For example, if your parent theme is called “storefront”, you can name your child theme directory “storefront-child”.
  2. In the new directory, create a file called style.css. This file will contain the styling for your child theme.
  3. Add the following code to your style.css file:

/*
Theme Name: Your Child Theme Name
Template: parent-theme-name
*/

/* Add your custom CSS below this line */

Replace “Your Child Theme Name” with the name you want to give your child theme, and “parent-theme-name” with the directory name of your parent theme.

Once you have created your child theme, go to your WordPress admin area and navigate to Appearance > Themes. Your child theme should now appear in the list of available themes. Activate your child theme to start using it.

Adding the PHP Snippet to the Functions.php File

Now that your child theme is set up, you can implement the PHP snippet to remove the register form in WooCommerce. To do this, you will need to create a functions.php file in your child theme directory.

Follow these steps to add the PHP snippet to your child theme:

  1. Create a new file in your child theme directory and name it functions.php.
  2. Open the functions.php file in a text editor and add the following code:

<?php

// Add your custom functions below this line

add_action('init', 'typek_remove_register_form');

function typek_remove_register_form() {
    if (class_exists('WooCommerce')) {
        remove_action('woocommerce_before_customer_login_form', 'woocommerce_output_registration_form_wrap');
        remove_action('woocommerce_after_customer_login_form', 'woocommerce_output_registration_form_wrap');
    }
}

?>

This code includes the PHP snippet we discussed earlier to remove the register form in WooCommerce.

Save the functions.php file and upload it to your child theme directory on your server.

Testing the Changes

With the PHP snippet added to your child theme’s functions.php file, the register form in WooCommerce should now be removed. To test the changes, follow these steps:

  1. Visit your “My Account” page on your WooCommerce site.
  2. Confirm that the register form is no longer displayed on the page.
  3. Try to access the registration form directly by navigating to yourdomain.com/my-account/?action=register (replace “yourdomain.com” with your actual domain name).
  4. Confirm that the registration form is still not displayed, and you are redirected to the login form.

If the register form is successfully removed, your changes have been implemented correctly.

Conclusion

In this tutorial, we discussed how to remove the register form in WooCommerce using a PHP snippet. We covered the steps to create a child theme, add the PHP snippet to the functions.php file, and test the changes on your site. By following these steps, you can easily remove the register form from your WooCommerce site and prevent users from creating new accounts.

More traffic??

Are you looking for effective solutions to increase your Google search visibility?

Contact us and discover the full potential of online sales.

What our clients are say?

For me, they are a happiness factory. They managed my store’s website in such a way that users became happy, after which it turned out that the search engine was also happy. When the search engine became happy and started bringing me more happy customers, my business became equally happy 🙂

Matt

Great company, I recommend it to everyone. Services of the highest standard. Very good customer approach. I am very satisfied with their work.

Lukasz Szajny

Complete professionalism and an incredible commitment to their work. It’s worth emphasizing their hassle-free communication and full availability. I definitely recommend them.

Alexander