Adding User Types to WordPress and BuddyPress – with User Blogs

Posted by on Oct 13, 2010 in Articles, How-To | 6 comments

There is a new version of this code located at http://cleverness.org/2011/01/27/updated-buddypress-user-roles-code/. Please use that code and leave all comments on that page.

I’ve previously talked about adding user types to BuddyPress. I’ve found out recently that if you allow new users to create their own blogs, you will need to add the following code to your functions.php file.

function synchro_wp_usermeta_blog($blog_id, $user_id, $domain, $path, $site_id, $meta) {
	global $bp, $wpdb;

	$type = $meta[signup_type];

	update_usermeta( $user_id, 'signup_type', $type );

}

add_action( 'wpmu_new_blog', 'synchro_wp_usermeta_blog', 10, 6 );

function register_role_blog($blog_id, $user_id, $domain, $path, $site_id, $meta) {
	global $bp, $wpdb;

   	$userdata = array();
   	$userdata['ID'] = $user_id;
   	$userdata['role'] = $meta['signup_type'];

   	//only allow if user role is my_role

   	if ($userdata['role'] == 'usertypea' || $userdata['role'] == 'usertypeb' || $userdata['role'] == 'usertypec' ){
      	wp_update_user($userdata);
   	}

}

add_action( 'wpmu_new_blog', 'register_role_blog', 101, 6 );

6 Comments

  1. Thanks to have checked Cindy
    I will follow your advice and check everything by me.
    I don’t use any plugin on this site except BP and the usuals ones.
    I will tell you
    cheers

  2. Hi Cindy

    Thanks for your very useful trick, it should be a successfull plugin !

    I implemented it on a WP MU 3.0.3 with BP 1.2.7 ; I used the plugin User Role Editor to setup roles.

    It works fine : new users are receving the role they have choosen when they signup.
    In my adminpanel, when I look at the users list, I can see how many users are assigned to one role, but when I click on the role upon the list, no user is showing. In the DB, the user is created with the proper role.

    Thanks in advance for any idea.
    Cheers

    • Sorry I was to quick on posting

      When I edit the user through the adminpanel (/wp-admin/user-edit.php?user_id=), I can see there is no role assigned to him.

      in the wp_user, statut is by 0. In the wp_usermeta, signup_type and wp_capabilities are showing the choosen role

      So it doesn’t work on my installation.
      ;-(

      Any ideas ?
      Thanks in advance
      Cheers

      • No idea at the moment. I will be taking a look at the user roles code at the end of the week so I might have something later.

        • thanks a lot
          The whole site I want to build with WP + BP lays on the possibility to distinguish users with different roles/capabilities and it could only happens by signup (not in the BP groups)
          cheers and happy new year

    • I’ve just tested the code (and used the User Role Editor plugin) and it’s working for me. I would suggest double checking the code from this page and my original user type post to make sure everything is correct. It does sound as if it’s correct though since the database is showing it. It appears in three places in the database: signup_type and wp_capabilties in the wp_usermeta table and in the meta field in wp_signups. If it’s correct in all three places, I would try disabling any other plugins besides BuddyPress and the User Role Editor to see if there’s something conflicting.

Leave a Reply

Your email address will not be published. Required fields are marked *