Skip to content

Instantly share code, notes, and snippets.

@paulgoodchild
Last active December 10, 2021 15:29
Embed
What would you like to do?
Add custom user roles to enforce 2FA by email using Shield Security plugin for WordPress
<?php
/**
* Adding custom roles is a case of using the filter provided, adding your
* roles to the array of roles that has 2FA by email forced upon them.
*
* The role you add will be the 'slug' of the role, not the name of the role.
* For example, WordPress comes with built-in roles such as Administrator.
* The slug for this role is 'administrator', not 'Administrator'.
*/
add_filter( 'odp-shield-2fa_email_user_roles', function ( $roles ) {
/**
* The role you add is the 'slug' for the role, not its pretty name.
*/
$roles[] = 'my-custom-role';
// To add multiple roles you can either:
// 1. add each role individually
$roles[] = 'my-custom-role-1';
$roles[] = 'my-custom-role-2';
$roles[] = 'my-custom-role-3';
// 2. Or merge 2 arrays (this is probably the neater method)
$roles = array_merge(
$roles,
[
'my-custom-role-1',
'my-custom-role-2',
'my-custom-role-3',
]
);
// ALWAYS return $roles.
return $roles;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment