Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like new users of the blog to have custom role, rather then Subscriber. How do I set this programmatically? I know that it can be changed from the backend.

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

This allows plugins to easily hijack the default role while they're active.

// Hijack the option, the role will follow!
add_filter('pre_option_default_role', function($default_role){
    // You can also add conditional tags here and return whatever
    return 'subscriber'; // This is changed
    return $default_role; // This allows default
});

I use it to make sure some plugins that need subscriber roles get it regardless of Administrator attempts to change :)

Regards.

share|improve this answer
add comment

When a new user is created through the wp_insert_user() function, the role is set with the set_role() method of the User class and is set to the default user role.

The default user role is retrieved from the database get_option('default_role') and can be set via the Admin Dashboard by going to Settings > General > New User Default Role.

If you've registered your new role (using add_role(), it should be available there to choose from.

Alternatively, for more control you can hook into the user_register or profile_update actions to then set_role on them conditionally, or however else you like.

share|improve this answer
    
I will mark your one as correct, although real answer turned out to be as simple as update_option('default_role', 'custom_role_name') upon activation of the plugin. –  jayarjo Oct 22 '11 at 9:05
    
Glad to have been of help. –  soulseekah Oct 22 '11 at 9:09
add comment

After you created the user using

$user_id = wp_create_user( $user_login, $pas1, $user_email );

Create the user object

$user = new WP_User($user_id);

Then set the role

$user->set_role('editor');

You can replace the 'editor' role with any of the following: Administrator,Editor,Author,Contributor,Subscriber

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.