Skip to content

Instantly share code, notes, and snippets.

@mattmcgiv
Last active January 11, 2017 16:04
Show Gist options
  • Select an option

  • Save mattmcgiv/6d2cb701bd59df6750be75a6dda76792 to your computer and use it in GitHub Desktop.

Select an option

Save mattmcgiv/6d2cb701bd59df6750be75a6dda76792 to your computer and use it in GitHub Desktop.
Add a role to all users
<?php
//Uncomment the following line for converting one role to another
//add_action( 'the_post', 'convert_subscribers_to_executives' );
function convert_subscribers_to_executives() {
//Note: this only works by navigating to the page with ID 5883 on the site
//to kick off the loop that updates all of the users.
if (get_the_ID() === 5883) {
//get all subscribers from this WP site
$blogusers = get_users( 'blog_id=1&orderby=nicename&role=subscriber' );
//Foreach user: add the Executive role (slug: executive) **in addition to** the current role.
foreach ($blogusers as $user) {
//wp_die(json_encode($user->ID));
$addMemberToGroup = new WP_User($user->ID);
$addMemberToGroup->add_role( 'executive' );
}
return;
}
else {
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment