The MU forums have moved to WordPress.org

Creating New Roles Sitewide (11 posts)

  1. mrzerog
    Member
    Posted 14 years ago #

    Hello all.
    I need to create two new user roles for my wpmu site:
    One that has the following capabilites:
    add/edit/delete users
    add/edit/delete pages/posts

    And one that just has the ability to edit posts.

    The first role must have the ability to create new users, but only users whose role is the second one I've described. I'm just learning about hooking into Wordpress, so I assume I would have to hook into the register hook?

    Thank you very much,
    Joe

  2. Wuogger
    Member
    Posted 14 years ago #

    Hey, I just posted an hour or so after you with the same issue. I made a plugin to solve it. For the hook I just added it to Admin head. It probably needs something that says if it's already set to not run?

    Here's my reference: http://wordpress.taragana.net/nav.html?_functions/index.html
    (click on Line 254)

    Here's my code:

    function add_director_role() {
    global $wp_roles;
    
    add_role('director',__('Director'));
    
    $role = get_role ('director');
    $role -> add_cap ('delete_others_pages');
    $role -> add_cap ('delete_others_posts');
    $role -> add_cap ('delete_private_pages');
    $role -> add_cap ('edit_private_posts');
    $role -> add_cap ('delete_published_pages');
    $role -> add_cap ('delete_published_posts');
    $role -> add_cap ('edit_files');
    $role -> add_cap ('edit_others_pages');
    $role -> add_cap ('edit_others_posts');
    $role -> add_cap ('edit_pages');
    $role -> add_cap ('edit_plugins');
    $role -> add_cap ('edit_posts');
    $role -> add_cap ('edit_private_pages');
    $role -> add_cap ('edit_private_posts');
    $role -> add_cap ('edit_published_pages');
    $role -> add_cap ('edit_published_posts');
    $role -> add_cap ('edit_themes');
    $role -> add_cap ('edit_users');
    $role -> add_cap ('moderate_comments');
    $role -> add_cap ('publish_pages');
    $role -> add_cap ('publish_posts');
    $role -> add_cap ('read_private_pages');
    $role -> add_cap ('read_private_posts');
    $role -> add_cap ('switch_themes');
    $role -> add_cap ('unfiltered_upload');
    $role -> add_cap ('upload_files');
    $role -> add_cap ('manage_options');
    $role -> add_cap ('read');
    $role -> add_cap ('level_0');
    $role -> add_cap ('level_1');
    $role -> add_cap ('level_2');
    $role -> add_cap ('level_3');
    
    }
    add_action ('admin_head','add_director_role')
  3. mrzerog
    Member
    Posted 14 years ago #

    Thanks Wuogger. With you help, I am able to get these two roles to show up in the curiously named Add New* panel under Users. (Which does not seem to add users at all, just control a few things about what happens when they ARE added, through the source file wpmu-options.php)

    The goal here is to add a role, and then have that role be the default role assigned to new users when new user/new blog are created. I can find the functions in question:

    add_user_to_blog( $blog_id, $user_id, $role )
    
    wpmu_create_blog($domain, $path, $title, $user_id, $meta = '', $site_id = 1)

    I assume I have to hook into one of those, but I am not sure if I can. Are those even 'hooks'?

  4. wpmuguru
    Member
    Posted 14 years ago #

    The 2 types of hooks to look for are apply_filter and do_action. More info is available about how to use them in the codex.

  5. mrzerog
    Member
    Posted 14 years ago #

    So, I've found the action 'add_user_to_blog', which takes 3 arguments. I need to change the value of one of the arguments, but I am not sure how to get at particular arguments from my plugin.
    For instance, here is add_user_to_blog

    function add_user_to_blog( $blog_id, $user_id, $role ) {
    	switch_to_blog($blog_id);
    
    	$user = new WP_User($user_id);
    
    	if ( empty($user) )
    		return new WP_Error('user_does_not_exist', __('That user does not exist.'));
    
    	if ( !get_usermeta($user_id, 'primary_blog') ) {
    		update_usermeta($user_id, 'primary_blog', $blog_id);
    		$details = get_blog_details($blog_id);
    		update_usermeta($user_id, 'source_domain', $details->domain);
    	}
    
    	$user->set_role($role);
    
    	do_action('add_user_to_blog', $user_id, $role, $blog_id);
    	wp_cache_delete( $user_id, 'users' );
    	restore_current_blog();
    	return true;
    }

    So I need to get at $role. Does my plugin then have to be set up like this:

    function change_user_role_to_something_else($blog_id, $user_id, $role)
    {
    //do stuff
    }

    and then I hook in like this:

    add_action('add_user_to_blog', 'change_user_role_to_something_else', 1, 3);

    I guess I am confused by how arguments are passed back and forth from hooks. The codex is mum, all it says is some hooks pass more arguments than others.

    Thanks,
    Joseph

  6. dsader
    Member
    Posted 14 years ago #

    if the do_action has args in a certain order and number, do your add_action args have to match same order? Anyway,

    Given
    do_action('add_user_to_blog', $user_id, $role, $blog_id);

    then plugin becomes
    add_action('add_user_to_blog', 'change_user_role_to_something_else', 10, 3);

    Where

    function change_user_role_to_something_else($user_id, $role, $blog_id) {
    //do stuff to $role, while ignoring $user_id and $blog_id
    return $role;
    }

    With this hook, any users added to any blog gets the new role.

  7. dsader
    Member
    Posted 14 years ago #

    Here's my working example where I want new users to only be contributor to their own blog while also being added as a subscriber to main blog:

    <?php
    function ds_new_user_meta($blog_id, $user_id) {
    // add user to the blog they've just created as contributor - overwriting previous administrator role
    add_user_to_blog($blog_id, $user_id, 'contributor' );
    // while we are at it we add every new user to blog_id 1 as subscriber
    add_user_to_blog('1', $user_id, 'subscriber');
    }
    add_action( 'wpmu_new_blog', 'ds_new_user_meta', 10, 2 );
    //priority 10 with 2 arguments in the 'wpmu_new_blog' do_action hook line 1309 of wpmu-functions.php
    ?>
  8. mrzerog
    Member
    Posted 14 years ago #

    Oh hey that's really neat! I think I am starting to get a grasp on this. So now I can insert the user as a contributor, which is very close to what I want to do. But I need to add / remove certain capabilities in order to be fully functional. Here's my budding plugin so far:
    http://codepad.org/ei3FvWpa
    I welcome any advice or grumbley best practice reminders ;)

  9. mrzerog
    Member
    Posted 14 years ago #

    So I am going to go out on a limb here and explain how I think adding new roles MIGHT be supposed to happen, I'm probably really wrong, but here goes:

    First I need to make a role, through
    add_role
    as above.
    Then I need to assign the role I just made to a variable,
    $role = get_role('the_one_I_just_made')
    then I need to assign it capabilities through
    $role->add_cap
    which is a method of the role object, also as shown above.

    Now that I've made the role, I need to get the data of the user to whom I wish to apply the role, possibly through

    $user = new WP_user($user_id_whose_data_I_want_to_get);

    Then I
    set_role
    on it, which removes it's old role(s) and gives it the new one. $user->set_role($role)

    Then... I save it somehow?

    As you can see, I have only a vague idea of what the process that I am trying to do consists of. Any helpful clarification of steps or pints to the right codex would be much appreciated.

    Thanks,
    joe

  10. dsader
    Member
    Posted 14 years ago #

    I've never tried to create roles sitewide, I have used a plugin to create/modify caps/role on a blog by blog basis with Role Manager. Give it a try on a single WP install and see if it has something to offer.

    Some discussion/suggestions on tweaking Role Manager for sitewide WPMU: http://mu.wordpress.org/forums/topic/9301?replies=14

    I went another route and more simply disabled menu's sitewide rather than fiddle with roles sitewide: http://wpmudevorg.wordpress.com/project/Menus

    Found a WPMU discussion of an install that uses both appoaches:
    http://openedweb.com/blog/2009/02/17/two-more-wpmu-plugins-for-k12/

  11. mrzerog
    Member
    Posted 14 years ago #

    Okay, I have edited my code above and changed:

    function edit_user_role($blog_id, $user_id)
    	{
    		add_user_to_blog($blog_id, $user_id, 'contributor');
    	}

    TO:

    function edit_user_role($blog_id, $user_id)
            {
    
                    $role = get_role('editor');
                    $role->remove_cap('manage_links');
                    $role->remove_cap('manage_categories');
                    add_user_to_blog($blog_id, $user_id, 'editor');
            }

    Called in the same way. This allows me to edit the created user's capabilities on the blog being installed.

    Thanks all,
    Joe

About this Topic