Skip to content

Instantly share code, notes, and snippets.

@Archie22is
Created August 17, 2022 17:58
Show Gist options
  • Select an option

  • Save Archie22is/259a892e190f734ee15b2e34c826467f to your computer and use it in GitHub Desktop.

Select an option

Save Archie22is/259a892e190f734ee15b2e34c826467f to your computer and use it in GitHub Desktop.
WordPress: modify user roles dynamically with functions.php
<?php
function custom_user_role() {
// get user
$user = new WP_User( <user-ID> );
//$user = new WP_User( '<user-login-name>' );
//$user = wp_get_current_user();
// modify roles
// for example, set/unset them as administrator
$user->add_role( 'administrator' );
//$user->remove_role( 'administrator' );
}
// register action
add_action( 'admin_init', 'custom_user_role' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment