WP 2fa Shortcodes

Below, you’ll find a detailed guide on how to add the WP 2fa Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Wp 2fa Plugin shortcodes not to show or not to work correctly.

Before starting, here is an overview of the Wp 2fa Plugin and the shortcodes it provides:

Plugin Icon
WP 2FA – Two-factor authentication for WordPress

"WP 2FA – Two-factor authentication for WordPress is a robust plugin designed to enhance security measures. It adds an extra layer of protection to your WordPress site by enabling two-factor authentication."

★★★★☆ (109) Active Installs: 50000+ Tested with: 6.3.2 PHP Version: 7.2
Included Shortcodes:
  • [wp-2fa-setup-form]
  • [wp-2fa-setup-notice]

WP 2fa [wp-2fa-setup-form] Shortcode

The WP-2FA plugin shortcode enables users to set up a two-factor authentication form. It supports a redirect_after attribute, allowing users to override all other settings. This code checks if a user is logged in. If true, it displays the 2FA configuration form. If the user isn’t logged in, it provides a login link.

Shortcode: [wp-2fa-setup-form]

Parameters

Here is a list of all possible wp-2fa-setup-form shortcode parameters and attributes:

  • show_preamble – Display introductory text before the form, set as ‘true’ by default
  • redirect_after – URL to redirect to after form submission, empty by default

Examples and Usage

Basic Example – Generates a form for setting up 2-Factor Authentication for a logged-in user

[wp-2fa-setup-form]

Advanced Examples

Generates a 2FA setup form and redirects the user to a specific page after setup. The ‘redirect_after’ attribute should be set to the URL of the desired page.

[wp-2fa-setup-form redirect_after='https://yourwebsite.com/after-setup-page/']

Generates a 2FA setup form without the preamble text. The ‘show_preamble’ attribute should be set to ‘false’.

[wp-2fa-setup-form show_preamble='false']

Generates a 2FA setup form without preamble and redirects the user to a specific page after setup. Both ‘show_preamble’ and ‘redirect_after’ attributes are used in this example.

[wp-2fa-setup-form show_preamble='false' redirect_after='https://yourwebsite.com/after-setup-page/']

PHP Function Code

In case you have difficulties debugging what causing issues with [wp-2fa-setup-form] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'wp-2fa-setup-form', array( __CLASS__, 'user_setup_2fa_form' ) );

Shortcode PHP function:

function user_setup_2fa_form( $atts ) {

			/** Shortcode redirect_after is supported, with which the user can override all other settings */
			extract( // phpcs:ignore
				shortcode_atts(
					array(
						'show_preamble'  => 'true',
						'redirect_after' => '',
					),
					$atts
				)
			);

			/**
			 * Fires when the FE shortcode scripts are registered.
			 *
			 * @param bool $shortcodes - True if called from the short codes method.
			 *
			 * @since 2.2.0
			 */
			\do_action( WP_2FA_PREFIX . 'shortcode_scripts', true );

			if ( is_user_logged_in() ) {
				wp_enqueue_script( 'wp_2fa_frontend_scripts' );
				wp_enqueue_style( 'wp_2fa_styles' );

				ob_start();
				echo '<form id="your-profile" class="wp-2fa-configuration-form">';
				User_Profile::inline_2fa_profile_form( 'output_shortcode', $show_preamble );
				echo '</form>';
				$content = ob_get_contents();
				ob_end_clean();
				return $content;
			} elseif ( ! is_admin() && ! is_user_logged_in() ) {
				ob_start();
				$new_page_id = WP2FA::get_wp2fa_setting( 'custom-user-page-id' );
				$redirect_to = ! empty( $new_page_id ) ? get_permalink( $new_page_id ) : get_home_url();
				$link_markup = '<a href="' . esc_url( wp_login_url( $redirect_to ) ) . '">' . esc_html__( 'Login here.', 'wp-2fa' ) . '</a>';
				$message     = '<p>' . str_replace( '{login_url}', $link_markup, WP2FA::get_wp2fa_white_label_setting( 'login-to-view-area', true ) ) . '</p>';
				echo wp_kses_post( $message );
				$content = ob_get_contents();
				ob_end_clean();
				return $content;
			}
		}

Code file location:

wp-2fa/wp-2fa/includes/classes/Shortcodes/class-shortcodes.php

WP 2fa [wp-2fa-setup-notice] Shortcode

The WP-2FA plugin shortcode ‘wp-2fa-setup-notice’ is designed to notify users to set up their two-factor authentication (2FA). It enqueues necessary scripts and styles, and generates a user notice.

Shortcode: [wp-2fa-setup-notice]

Examples and Usage

Basic example – A simple use of the shortcode to display the 2FA setup notice. This example doesn’t require any parameters.

[wp-2fa-setup-notice /]

Advanced examples

Using the shortcode with the ‘configure_2fa_url’ attribute. This attribute allows you to specify a custom URL where the user can configure their 2FA settings. If not provided, the default URL will be used.

[wp-2fa-setup-notice configure_2fa_url="https://yourwebsite.com/custom-2fa-setup-url" /]

Using the shortcode within a PHP echo function. This is useful when you want to include the shortcode within your PHP templates.

<?php echo do_shortcode('[wp-2fa-setup-notice configure_2fa_url="https://yourwebsite.com/custom-2fa-setup-url" /]'); ?>

PHP Function Code

In case you have difficulties debugging what causing issues with [wp-2fa-setup-notice] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'wp-2fa-setup-notice', array( __CLASS__, 'user_setup_2fa_notice' ) );

Shortcode PHP function:

function user_setup_2fa_notice( $atts ) {
			extract( // phpcs:ignore
				shortcode_atts(
					array(
						'configure_2fa_url' => '',
					),
					$atts
				)
			);

			// TODO: is that really necessary?
			User_Notices::init();

			if ( ! is_admin() && is_user_logged_in() ) {
				wp_enqueue_script( 'wp_2fa_micro_modals' );
				wp_enqueue_script( 'wp_2fa_frontend_scripts' );
				wp_enqueue_style( 'wp_2fa_styles' );

				$data_array = array(
					'ajaxURL'        => admin_url( 'admin-ajax.php' ),
					'roles'          => WP2FA::wp_2fa_get_roles(),
					'nonce'          => wp_create_nonce( 'wp-2fa-settings-nonce' ),
					'codesPreamble'  => esc_html__( 'These are the 2FA backup codes for the user', 'wp-2fa' ),
					'readyText'      => esc_html__( 'I\'m ready', 'wp-2fa' ),
					'codeReSentText' => esc_html__( 'New code sent', 'wp-2fa' ),
					'allDoneHeading' => esc_html__( 'All done.', 'wp-2fa' ),
					'allDoneText'    => esc_html__( 'Your login just got more secure.', 'wp-2fa' ),
					'closeWizard'    => esc_html__( 'Close Wizard', 'wp-2fa' ),
				);
				wp_localize_script( 'wp_2fa_frontend_scripts', 'wp2faData', $data_array );

				ob_start();
				User_Notices::user_setup_2fa_nag( 'output_shortcode', $configure_2fa_url );
				$content = ob_get_contents();
				ob_end_clean();

				return $content;
			}

			return '';
		}

Code file location:

wp-2fa/wp-2fa/includes/classes/Shortcodes/class-shortcodes.php

Conclusion

Now that you’ve learned how to embed the Wp 2fa Plugin shortcodes, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

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