The MU forums have moved to WordPress.org

Add Users to Blogs - Different Roles (15 posts)

  1. jacob00
    Member
    Posted 14 years ago #

    I'm using the following code suggested on these forums to add new users to selected blogs. It works great. While the option exists to have new users added with a specific role (in this case Contributor), ideally I'd like to have new users added to the primary blog in one role, but for the remainder in another role (author in primary and contributor in the rest). Any ideas?

    <?php
    add_action( 'wpmu_new_user', 'ds_new_user_meta', 10, 2);
    function ds_new_user_meta($user_id) {
    $blogs = array(1,2,3,4,5);
    foreach ($blogs as $blog) {
    add_user_to_blog($blog, $user_id, 'contributor');
    }
    // adds every new user to blog_id 1 to 5
    } ?>
  2. dsader
    Member
    Posted 14 years ago #

    If by "primary" you mean blog 1 then...

    <?php
    add_action( 'wpmu_new_user', 'ds_new_user_meta', 10, 2);
    function ds_new_user_meta($user_id) {
    add_user_to_blog('1', $user_id, 'author');
    // adds every new user to blog_id 1 as author
    $blogs = array(2,3,4,5);
    foreach ($blogs as $blog) {
    add_user_to_blog($blog, $user_id, 'contributor');
    }
    // add user to blog_id 2-5 as contributor
    } ?>
  3. jacob00
    Member
    Posted 14 years ago #

    Works great. Thanks!

  4. jacob00
    Member
    Posted 14 years ago #

    dsader

    In this instance, I'd also like to prevent new users from seeing the dashboard of all but the primary blog. Any idea how I might do that? I'm using Menus that can prevent everything but the Profile page, but ideally I would hide the profile area as well for all but the primary blog.

    Thx

  5. dsader
    Member
    Posted 14 years ago #

    I haven't tried to alter the behaviour of the Menus plugin with a check for the user's primary blog first. You could add a check for the primary blog easily enough I figure before it goes ahead and hides the dashboard menu. I'm not interested in doing that, though, simply because I wouldn't use it that way. I have users that are admins of unlimited blogs, so I need the plugin to behave as is on every blog - primary or otherwise. Good luck.

  6. jmunn
    Member
    Posted 14 years ago #

    This seemed to work well for what I wanted to do as well but when someone registers via Gigya Socialize plugin this no longer works. They just become subscribers. Any ideas? I'm only enabling the twitter and facebook logins and am not sure what makes these users different from users that do the typical registering process.

    I'm hoping to allow all users to be authors on all blogs, even future ones that I create, except for blog_id 1. Any help is much appreciated.

  7. wpmuguru
    Member
    Posted 14 years ago #

    I would assume that Gigya Socialize does not use the standard user registration process for MU. That would explain why the otehr plugin doesn't get applied.

  8. jmunn
    Member
    Posted 14 years ago #

    Thanks,
    That certainly seems like a safe assumption. Unfortunately I'm not so code savvy so I was hoping it was something fairly straightforward for those with more skills and experience. Do you have any suggestions as to what kind of code in that plugin I should start looking for?
    Thanks again.

  9. nickohrn
    Member
    Posted 14 years ago #

    @jmunn - I'm the developer for the Gigya Socialize for WordPress plugin. We use the standard user registration functions, but not the wpmu_new_user function. We use wp_insert_user instead.

    A simple solution to your problem seems to be to hook to the action user_register instead of wpmu_new_user. Please try that and let me know if it works.

  10. jmunn
    Member
    Posted 14 years ago #

    Nick this worked great. Thank you very much.
    Now I'm struggling with dsader's code above trying to make all users authors on all current blogs, as well as future ones created by anybody. I'm using a "default role" plugin that makes everyone an author when they signup but I'm not sure how to tweak the above code to get them on all present and future blogs. Can anyone help?

  11. holo-alan
    Member
    Posted 14 years ago #

    I have found this bit of code to add all users into every blog (above and below) as a subscriber and it works well. But is there a way to tell it to add to every blog instead of me having to list them all in the $blogs array?

    add_action( 'user_register', 'ds_new_user_meta', 10, 2);
    function ds_new_user_meta($user_id) {
    $blogs = array(1,2,3,4,5,6,7,8,9,10);
    foreach ($blogs as $blog) {
    add_user_to_blog($blog, $user_id, 'subscriber');
    }
    // adds every new user to blog_id 1 to 10
    }
  12. dsader
    Member
    Posted 14 years ago #

    I can't test this but ...

    add_action( 'user_register', 'ds_new_user_meta', 10, 2);
    function ds_new_user_meta($user_id) {
    //$blogs = array(1,2,3,4,5,6,7,8,9,10);
    $blogs = $wpdb->get_results( $wpdb->prepare("SELECT blog_id FROM $wpdb->blogs WHERE site_id = %d AND public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' ORDER BY registered DESC", $wpdb->siteid), ARRAY_A );
    foreach ($blogs as $blog) {
    add_user_to_blog($blog['blog_id'], $user_id, 'subscriber');
    }
    // adds every new user to all "public" blogs
    }
  13. holo-alan
    Member
    Posted 14 years ago #

    thanks for this, I will test it and see if it does it ok.

    thanks again..

  14. holo-alan
    Member
    Posted 13 years ago #

    This code works well when I manually create users but not when they are created by a plugin I use to manage memberships..

    Is there a difference when (or different way for users to be created) a plugin creates a user?

  15. holo-alan
    Member
    Posted 13 years ago #

    Just FYI this looks like the function of the plugin that is creating the new user, but I am only guessing my php isn't that good...

    Is there a way to add the code above into this, so the new user gets added to all blogs???

    Thanks in Advance..

    if (!function_exists('byrd_create_user')){
    	function byrd_create_user( $username, $email, $password = false ){
    
    		$_tbl =& bTable::getInstance('users', 'Table');
    
    		if (!$password) $password = byrd_generate_password( 12, false );
    
    		//save this to the database
    		$_tbl->bind( array(
    			'user_login' => $username,
    			'user_pass' => byrd_hash_password($password),
    			'user_nicename' => $username,
    			'user_email' => $email,
    			'user_registered' => date('Y-m-d H:i:s'),
    			'user_status' => 0,
    			'display_name' => $username
    
    		) );
    		$_tbl->store();
    		return array('user_id' => $_tbl->ID, 'password' => $password);
    	}
    
    }

About this Topic

  • Started 14 years ago by jacob00
  • Latest reply from holo-alan