You can disable magic login for a specific user easily by following these steps:
- Navigate user profile
- In their profile settings, find the option labeled “Disable Magic Login for this user.”
- Check the box next to this option to disable magic login for the user.
- Click “Update User” to apply the changes.
By default only users who has “edit_users” capability have access this feature, you can use magic_login_current_user_can_control_user
filter to control capability.
For example, if you want to allow subscriber-level users to disable/enable magic login, you can use this snippet:
function can_current_user_control_magic_login($status) {
$current_user = wp_get_current_user();
return current_user_can('edit_user', $current_user->ID);
}
add_filter('magic_login_current_user_can_control_user', 'can_current_user_control_magic_login');
Since Magic Login stores metadata to determine whether it’s disabled for a user, you can programmatically control this behavior by filtering the meta value. For example:
add_filter( 'get_user_metadata', function ( $value, $user_id, $meta_key, $single ) {
// Check if the requested metadata is for the magic login disable feature
if ( $meta_key === \MagicLogin\Constants\DISABLE_USER_META ) {
// Get the user object
$user = get_userdata( $user_id );
// Check if the user has the 'administrator' role
if ( in_array( 'administrator', (array) $user->roles, true ) ) {
return true; // Disable magic login for admins
}
}
return $value;
}, 10, 4 );
(disables magic login for admin users)