Creating a WP 2FA tab within the WooCommerce My Account dashboard

Last updated on September 28th, 2021 by Daniel Jones. Filed under WordPress Security Tutorials & Tips

Featured image *Add 2FA to your WooCommerce store*

If you’re running an online business using WooCommerce, ensuring your site’s security is of paramount importance. While security requires a 360-degree approach with continuous monitoring, improving, testing, and hardening, low-hanging fruit such as user 2FA authentication can protect you from security breaches due to weak passwords.

Thankfully, our WP 2FA plugin makes this a breeze. If you’re not already using it, I strongly suggest doing so today, so you can increase your site’s security in a matter of minutes.

This article will show you how you can enhance your WooCommerce store’s security by adding our 2FA configuration form to a separate tab within your users’ “My Account” area, producing a clean interface and making it easier for your users and customers alike to enable and use 2FA.

The code

We will begin this process by adding the code to your theme’s functions.php file or your site specific plugin (read creating your own site-specific plugin for more information on this subject). The entire process consists of two steps:

Step 1: Creating and catching your custom WooCommerce endpoint

WooCommerce endpoints are URL appendages that allow WordPress to show specific content without the need to have a separate webpage. For example, the page /my-account/ has several endpoints, such as:

  • /order/
  • /edit-account/
  • /lost-passwords/

These endpoints are appended to the actual URL, allowing content to be served to the client without requiring its own web page.

Therefore, we first need to create a custom endpoint. This custom endpoint will be the URL your users will visit to see the 2FA tab’s content.

// Create custom 2FA endpoint.
function wp_2fa_add_custom_config_endpoint() {
add_rewrite_endpoint( '2fa-config', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'wp_2fa_add_custom_config_endpoint' );
function wp_2fa_custom_query_vars( $vars ) {
$vars[] = '2fa-config';
return $vars;
}
add_filter( 'query_vars', 'wp_2fa_custom_query_vars', 0 );

Using the code above, your 2FA configuration page URL will read “domain.com/my-account/2fa-config/”. If you wish to change this, you can modify the variable value in the code provided above.

Step 2: add the link in the WooCommerce My Account page (user dashboard)

Now that we have set up the custom endpoint, we can add the link to users’ dashboards and the content for the new tab.

Please note, if you customized the “2fa-config” slug in the above code, be sure to do the same in the code below as well.

// Add link to tab.
function wp_2fa_custom_my_account_label( $items ) {
$items['2fa-config'] = '2FA Configuration';
return $items;
}

add_filter( 'woocommerce_account_menu_items', 'wp_2fa_custom_my_account_label' );

// Add tab content.
function wp_2fa_custom_account_content() {
echo do_shortcode( '[wp-2fa-setup-form]' );
}

add_action( 'woocommerce_account_2fa-config_endpoint', 'wp_2fa_custom_account_content' );

For simplicity, in the tab content, we use the shortcode provided by the plugin; however, you can, of course, expand on this however you wish. Refer to the WP 2FA plugin shortcodes for more information on all the shortcodes and customization options available.

Once you have added the above to your functions.php file or site-specific plugin, you will now see the new tab appear within your “My Account” dashboard, and your users will be able to add a further layer of security to their accounts in no time at all.

To sum up

Two-factor-authentication is a great addition to the security of your website, and you should certainly add it. However, 2FA is just one part of the game.

If you have customers without 2FA, make sure to enforce strong passwords. Then, of course, there are many other things you need to do in order to keep WooCommerce and your WordPress website secure.

Leave a Reply

Your email address will not be published. Required fields are marked *

Our other plugins