Skip to content

Instantly share code, notes, and snippets.

@mustardBees
Created November 15, 2022 16:02
Show Gist options
  • Save mustardBees/9b9b840dfc778d031a778f91c1422f67 to your computer and use it in GitHub Desktop.
Save mustardBees/9b9b840dfc778d031a778f91c1422f67 to your computer and use it in GitHub Desktop.
<?php
/**
* Enable email-based 2FA by default.
*
* @author pjv
* @link https://link.from.pw/3P7cH9u
*
* @param $providers
* @param $user_id
* @return mixed
*/
function pw_default_2fa( $providers, $user_id ) {
$force_roles = array(
'administrator',
'editor',
);
$user = get_user_by( 'id', $user_id );
if ( empty( array_intersect( $force_roles, $user->roles ) ) ) {
return $providers;
}
if ( empty( $providers ) && class_exists( 'Two_Factor_Email' ) ) {
$providers[] = 'Two_Factor_Email';
}
return $providers;
}
add_filter( 'two_factor_enabled_providers_for_user', 'pw_default_2fa', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment