Skip to content

Instantly share code, notes, and snippets.

@paulgoodchild
Last active May 20, 2024 12:48
Show Gist options
  • Save paulgoodchild/61e2fdd8aefd46792d055d13502109de to your computer and use it in GitHub Desktop.
Save paulgoodchild/61e2fdd8aefd46792d055d13502109de to your computer and use it in GitHub Desktop.
Intercept and prevent Shield's 2FA email sending process
<?php declare( strict_types=1 );
/**
* Perform any secondary checks before Shield verifies the 2FA nonce.
* If any of your checks fail, throw a new \Exception() with the error message you'd like to display to user.
*/
add_action( 'shield/2fa/email/pre_send_email/pre_nonce_verify', function ( \WP_User $user, string $plainNonce ) {
// e.g. this is a trivial example and not an actual check that is required.
// The message 'User ID is invalid.' will be displayed to the user.
if ( $user->ID < 1 ) {
throw new \Exception( 'User ID is invalid.' );
}
if ( \strlen( $plainNonce ) > 100 ) {
throw new \Exception( 'Nonce value is too long.' );
}
}, 10, 2 );
/**
* Alternatively, you can perform any further checks after Shield has verified the 2FA nonce.
*/
add_action( 'shield/2fa/email/pre_send_email/nonce_verified', function ( \WP_User $user, string $plainNonce ) {
// e.g. this is a trivial example and not an actual check that is required.
// The message 'User ID is invalid.' will be displayed to the user.
if ( $user->ID < 1 ) {
throw new \Exception( 'User ID is invalid.' );
}
if ( \strlen( $plainNonce ) > 100 ) {
throw new \Exception( 'Nonce value is too long.' );
}
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment