The MU forums have moved to WordPress.org

How to automatically add ALL new users to main blog? (36 posts)

  1. agreda
    Member
    Posted 4 years ago #

    The Dashboard Blog option reads ...

    New users are added to this blog as subscribers (or the user role defined below) if they don't have a blog.

    But, here is my issue ...

    I've noticed that new users who do create their own blog upon sign-up are not being added to our main blog.

    We've left our "Dashboard Blog" option blank, so users who do not create a blog are getting added to the main blog.

    But we are running Simple:Press forums on the main blog only so we need all new users to be added as main blog subscribers, so they become forum members by default. Whether they create their own blog upon sign-up or not.

    How can I make this happen automatically – without having to manually add users who have just registered, to the main blog, sending them yet another confirmation email?

    Thank you all in advance for your help!

    PS: Our site is at http://tripawds.com

  2. dsader
    Member
    Posted 4 years ago #

    I'd add the following two mu-plugins.

    First to add new users to blog 1:

    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, 'subscriber');
    }

    Second to migrate all existing users to blog 1:
    Install WPMU PowerTools in mu-plugins and "Run this code once" the following snippet:

    global $wpdb;
    	$query = "SELECT ID FROM {$wpdb->users}";
    	$users = $wpdb->get_results( $query, ARRAY_A );
    foreach($users as $user) {
    add_user_to_blog('1', $user['ID'], 'subscriber');
    }
  3. agreda
    Member
    Posted 4 years ago #

    @dsader Thank you for the quick response! I took care of migrating existing users in our recent migration from wp to wpmu, so I'll keep that Power Tools tip in mind for our next major move project.

    But regarding your first tip, exactly what file gets edited there? You mention two mu plugins. And forgive my ignorance, but what do the 10, 2 values represent?

    Thanks again.

  4. dsader
    Member
    Posted 4 years ago #

    The first is a new plugin for mu-plugins. Create a new php file and upload it to mu-plugins.

    The 10 is the load order $priority, 10 is default by the way. When many add_actions are added to the same do_action, the order each fires may be an issue. In this case, order is no issue.

    2 denotes the number of $accepted_args the function takes, which in the above example is wrong, should be 1 - my bad. 1 is the default as well.

    Therefore,
    add_action( 'wpmu_new_user', 'ds_new_user_meta');
    would work just the same.

    http://codex.wordpress.org/Function_Reference/add_action

  5. layotte
    Member
    Posted 4 years ago #

    For some reason I cannot get this to work for me...

    function ds_new_user_meta($user_id) {
    add_user_to_blog('1', $user_id, 'contributor' );
    }
    add_action( 'wpmu_new_user', 'ds_new_user_meta' );

    is in a file called new_user.php.

    It works fine if I change the blog id to 3:

    function ds_new_user_meta($user_id) {
    add_user_to_blog('3', $user_id, 'contributor' );
    }
    add_action( 'wpmu_new_user', 'ds_new_user_meta' );

    It's just not allowing me to add it to the main site. Any ideas/tips? Using WPMU 2.8.3 + BuddyPress 1.0.1.

    Also, I've put in some fwrites in the add_user_to_blog function in wpmu-functions.php to get a little debug info. Outputting blog_id, user_id, role, domain:

    1
    61
    contributor
    domain.com

    56
    61
    administrator

    So it's obviously getting to the point where it's trying to add the user to blog id 1, as the correct role.

  6. layotte
    Member
    Posted 4 years ago #

    Ok... so I got it working.

    function ds_new_user_meta($blog_id, $user_id) {
    add_user_to_blog('1', $user_id, 'contributor' );
    }
    add_action( 'wpmu_new_blog', 'ds_new_user_meta', 10, 2 );

    Seems to be working like this...

  7. agreda
    Member
    Posted 4 years ago #

    Thanks! but please excuse my ignorance...

    I presume I can just change 'contributor' to 'subscriber' to change their role. That's easy enough.

    But more importantly, where does this code go? Is it a core file (new_user.php) edit like layotte suggests? Or, does it go in mu-plugins as per dsader's instructions? And if the latter, what esle needs to be added to the file? (I've never written a plugin obviously!)

    Thanks for your help and patience ... we're learning here!

  8. layotte
    Member
    Posted 4 years ago #

    agreda,

    I created the new_user.php file, added the code to that file, and placed the code in mu-plugins (as dsader recommended).

    Don't forget to start and end the file with <?php and ?> (respectively).

    Hope that helps.

    Lew

  9. layotte
    Member
    Posted 4 years ago #

    Oh, and yes, you can change 'contributor' to 'subscriber'...

  10. agreda
    Member
    Posted 4 years ago #

    Thanks! I really appreciate the education. Will give it a shot.

  11. ToreGu
    Member
    Posted 4 years ago #

    This was very important to me! Please make this a part of WP canonical!

  12. ToreGu
    Member
    Posted 4 years ago #

    Hi again!

    I thought I got this working but I was wrong.

    I'm using the latest WPMU and also Buddypress. Buddypress seems to be quite dissatisfied with this and gives this error message:

    Warning: Cannot modify header information - headers already sent by (output started at /home/xxxxx/public_html/xxxxxx.com/wp-content/mu-plugins/new-user.php:8) in /home/xxxx/public_html/xxxx.com/wp-content/plugins/buddypress/bp-xprofile/bp-xprofile-signup.php on line 5

    And the graphics in the blog looks wrong.

    I've added this code in new_user.php (in wordpress_mu plugins folder):

    < php ? (but so that it works)

    add_action( 'wpmu_new_user', 'ds_new_user_meta', 10, 1);
    function ds_new_user_meta($user_id) {
    			add_user_to_blog('2', $user_id, 'author');
    }

    ?>

  13. dsader
    Member
    Posted 4 years ago #

    Remove the blank lines. The error says there is a problem with line 8, but the snip above has only 4 lines. Extra white space before or after php tags is not so good.

    http://codex.wordpress.org/FAQ_Troubleshooting#How_do_I_solve_the_Headers_already_sent_warning_problem.3F

  14. ToreGu
    Member
    Posted 4 years ago #

    Thanks for the help. I tried a lot with the file but had no success. I restarted with a new file and voilá.

    And also thanks for the code! Works perfect!

  15. jlm99
    Member
    Posted 4 years ago #

    This is terrific, and I've added it, BUT it doesn't seem to work if a sub-blog administrator adds a new user in their own blog's backend. Is there a second action hook needed for that? I'm trying to track it down myself, but any pointers would help!

  16. PBO
    Member
    Posted 4 years ago #

    I finally got this to work thanks for posting this in the thread above. BUT I Have a problem

    using this PHP code

    <?php add_action( 'wpmu_new_user', 'ds_new_user_meta', 10, 1);
    function ds_new_user_meta($user_id) {
    			add_user_to_blog('2', $user_id, 'subscriber');
    }
    ?>

    I have a main blog and a forums.domain.com

    users now create a blog and is automatically added in the forum blog which ID is 2.

    Now the problem is when new users sign the forums blogs becomes the primary blog and not the blog they have created. Is there a way to fix this.

    Thanks

  17. PBO
    Member
    Posted 4 years ago #

    Here is the solution

    <?php add_action( 'wpmu_new_user', 'ds_new_user_meta', 10, 1);
    function ds_new_user_meta($user_id) {
    add_user_to_blog( '2', $user_id, get_site_option( 'default_user_role', 'subscriber' ) );
    update_usermeta($user_id, 'primary_blog', $blog);
    }
    ?>
  18. jlappas
    Member
    Posted 4 years ago #

    I need to allow users to register to blog = 2 but not automatically be added to primary blog = 1. I I have tried to use this code in mu-plugins:

    <?php add_action( 'wpmu_new_user', 'ds_new_user_meta', 10, 1);
    function ds_new_user_meta($user_id) {
    add_user_to_blog( '2', $user_id, get_site_option( 'default_user_role', 'subscriber' ) );
    update_usermeta($user_id, 'primary_blog', $blog);
    }
    ?>

    This code allows new users to sign up in blog 2 correctly but it also registering them for the primary blog 1. How can I get them to only register for blog 2 without being added to blog 1. I have tried adding a line to remove the user from blog 1 within this code but have not been successful. I think WP code automatically adds all users to blog 1. Can you please help?

  19. agreda
    Member
    Posted 4 years ago #

    I need to allow users to register to blog = 2

    Why not just allow them to register by setting the "users can register" value to 1 for blog id 2? Just visit /wp-admin/wpmu-blogs.php?action=editblog&id=2 and look under Blog options (wp_2_options).

    Someone else might be able to confirm if this will do the trick, but I believe that is all that is needed to allow users to register for a specific blog. No plugin necessary! Good luck.

  20. dsader
    Member
    Posted 4 years ago #

    jlappas, yes WPMU adds users to blog 1 automagically, if there is no "dashboard blog" set in SiteAdmin->Options. If blog 2 is your "Dashboard Blog" change the setting there and no plugins are needed.

    Look for

    add_user_to_blog( '1', $user_id, get_site_option( 'default_user_role', 'subscriber' ) );

    in wp-includes/wpmu-functions.php

    remove_user_from_blog should still work in a plugin during the wpmu_new_user action perhaps at a later priority, change "10" to 99:

    http://codex.wordpress.org/Function_Reference/add_action

    agreda's "users can register" suggestion shouldn't work as there is no blog by blog registration.php in WPMU. wp-signup.php redirects signups from the main blog only.

  21. agreda
    Member
    Posted 4 years ago #

    Apologies for the misunderstanding. I realized later by "I need to allow users to register to blog = 2", jlappas actually meant, upon signup.

    It is my understanding that admins can use this setting to allow already registered site members to register for individual blogs.

  22. circuit
    Member
    Posted 4 years ago #

    i found this thread after posting my problem; users who create a blog are removed from the main blog.

    http://mu.wordpress.org/forums/topic/15375?replies=2

    i have almost exactly the same issue, but none of the above solutions work.

    thought i would create a link between the two topics in case this is useful for others in the same situation as me.

  23. jlappas
    Member
    Posted 4 years ago #

    Big help. I have it all working now. Thanks guys.

    dsader - options did the trick. I also followed the post in http://mu.wordpress.org/forums/topic/15375?replies=2

    Worked to help keep subscribers on blog 1 when new blog is created (this was confusing at first because I thought it was happening when users were added with no blog created.)

    Thanks again!

  24. deez
    Member
    Posted 4 years ago #

    Hi hope someone can help me

    When i create the newuser.php file and copy it to mu-plugins it makes the site error with:

    Parse error: syntax error, unexpected $end in .....wp-content/mu-plugins/new_user.php on line 1

    the code i am using in the .php is:

    <?php
    add_action( 'wpmu_new_user', 'ds_new_user_meta', 10, 2);
    function ds_new_user_meta($user_id) {
    $blogs = array(2,3);
    foreach ($blogs as $blog) {
    add_user_to_blog($blog, $user_id, 'subscriber');
    }
    // adds every new user to blog_id 2 and 3
    }
    ?>

  25. agreda
    Member
    Posted 4 years ago #

    @deez: Your code is much different than what we're using to successfully add every new user to blog id 1 as a subscriber.

    Hope this helps...

    <?php
    
    function ds_new_user_meta($blog_id, $user_id) {
    add_user_to_blog('1', $user_id, 'subscriber' );
    }
    add_action( 'wpmu_new_blog', 'ds_new_user_meta', 10, 2 );
    
    ?>
  26. andrea_r
    Moderator
    Posted 4 years ago #

    Deanna's new blog defaults plugin has this built right in.
    http://wordpress.org/extend/plugins/wpmu-new-blog-defaults/

  27. DeannaS
    Member
    Posted 4 years ago #

    @andrea_r - that one only runs when a new blog is created, though. If they want one that runs whenever a new USER is created, they want this one:
    http://wordpress.org/extend/plugins/wpmu-default-user-role/

  28. deez
    Member
    Posted 4 years ago #

    Thanks for the reply agreda

    I got that code from another similar thread which was closed so decided to post in this one.

    Anyway i pasted the same contents into an existing .php file and renamed it and the plugin now works to register new users to all my blogs.

    Is there something I need to know about creating .php files? i can simply rename a .txt to .php right...?

  29. agreda
    Member
    Posted 4 years ago #

    Sure, and as long as the code is correct it should do it's job.

  30. vyasritesh
    Member
    Posted 4 years ago #

    i created wordpress multiuser project.when any user creates its own blog then they have admin dashboard with all options same as a main wordpress blog administrator’s dashboard…. i want to hide some option of new blog’s users dashboard…….plz help me……

About this Topic