The MU forums have moved to WordPress.org

Default Role (31 posts)

  1. JoshEH
    Member
    Posted 7 years ago #

    On my install, when a new user signs up for just a user name, by default they seem to have no role in any blog. I'd like them to be level 0 / subscribers to the main blog by default.
    I saw someone else had a similar question about defaulting people to blog authors, but there didn't appear to be a final solution to the thread.
    Anyway, know exactly where this sort of change could be made?

  2. drmike
    Member
    Posted 7 years ago #

    I can't give you an exact answer here as I'm on a locked down terminal but take a look at the wp-includes/wpmu-functions.php file. That's where all of the user and blog creation occurs.

  3. JoshEH
    Member
    Posted 7 years ago #

    I've been looking all around for it, but haven't came across the right section yet. I found the activation stuff, but I cannot seem to find the part that injects information to the 'usermeta' table defining capabilities.

  4. JoshEH
    Member
    Posted 7 years ago #

    Got it!!
    It's a hack for sure, but here's how I did it:

    I added the following function call:

    addUser2MainBlog($user_id, 'wp_1_capabilities', 'a:1:{s:6:"author";b:1;}');

    inside the 'wpmu_create_user' (line 1092 ish) and 'wpmu_create_blog' (line 1123 ish) functions in 'wpmu-functions.php'.

    This allows all new users and blog owners access to the main blog as authors. If you want them to be simple subscribers replace the function call with:

    addUser2MainBlog($user_id, 'wp_1_capabilities', 'a:1:{s:10:"subscriber";b:1;}');

    You would have to include the following function I hacked together as well, I just placed this function between the other 2 mentioned here:

    function addUser2MainBlog($theID, $theMeta, $theValue) {
    global $wpdb;
    $wpdb->query("INSERT INTO wp_usermeta ( user_id, meta_key, meta_value ) VALUES ( '$theID', '$theMeta', '$theValue' )");
    }

    So it's messy, but it does work for what I need. Improvements?

  5. merqtio
    Member
    Posted 7 years ago #

    Hey JoshEH,

    How would you alter the hack so it would deal only with all blogs except the main blog? It would be nice to have new users to main blog be just subscribers, but have new users to the sub-blogs be authors...

    Merqtio

  6. merqtio
    Member
    Posted 7 years ago #

    Here's another thread trying to deal with this issue...

  7. suburbia
    Member
    Posted 7 years ago #

    JoshEH, many thanks for the hack, I've just stitched it into my blog and it's working well, one problem I have noticed however is the 'options' menu users are given after logging in, along with 'Dashboard' and 'Profile'. When clicked on, you get the 'sufficient' error again, is there a way to not show this menu item? I'll look through the code and try to do a similar 'hack' as yours, are you experiencing the same issue? (I added the 'subscriber' code rather than the 'author' in your post)

    Cheers

  8. JoshEH
    Member
    Posted 7 years ago #

    Sorry guys, I forgot to keep up checking on the thread I started. Hopefully it isn't too late. Anyway ...

    Let me take a look at the two questions presented and get back to you later today, and yes I have the same issue suburbia.

  9. JoshEH
    Member
    Posted 7 years ago #

    I've been looking around and cannot seem to find a way to make the options menu not show up. I did check and it doesn't happen in regular WordPress, so who changed the code around. lol

    It is kinda annoying, but did you notice that if the user has a blog and then logs into the main blog and then clicks options it sends them to the admin screen for their own blog. It's a bug, no idea how to fix it.

    merqtio, the only way off the top of my head that I can think of is to expand my hack to cycle through adding permissions to each blog individually, though that doesn't sound efficient.

    Now it does appear that any blog owner can add another blog owner/user as a subscriber/author/edt by entering their email address on the user panel. Also, any registered member can already login and comment on any blog if the blog owner requires users to be registered to comment.

    I'd love an idea on how to fix the options menu so that it doesn't show up. I suppose as a last resort one could make the error message more informative or perhaps add a check on the page to redirect users to the profile tab which is essentially their options page.

    As for the permissions, my hack works for what I need for the current site I am working on and probably the second MU install I plan to start next week. I'd love to see a built-in default permissions feature though instead of my hack. As well as options for blog owners to not allow users registered on other parts of the blog system to comment unless they have been signed up to the owner's blog. Does that make sense? Blog User 1 cannot comment on User 2's blog unless he has signed up for User 2's, assuming only registered users are allowed to comment.

  10. JoshEH
    Member
    Posted 7 years ago #

    I thought of something, don't ask me where to do this from yet, but when they login it does a permission check, maybe you can simply add an else statement.

    Ex:
    If user A has no permissions for this blog assume they have subscriber permissions.

  11. dimonprodigy
    Member
    Posted 7 years ago #

    Hi to all!
    I have similar problem.
    I need this: when new user is registered, he receives the Author role is his own blog, instead of the Administrator role (as by default)
    Any ideas? :)

    PS
    In WordPress there is option 'New User Default Role' but WordPressMU has no such option... :(
    /sorry for my poor English

  12. merqtio
    Member
    Posted 7 years ago #

    JoshEH said:

    It's a hack for sure... it's messy, but it does work for what I need. Improvements?

    JoshEH, this code below seems to work to allow default role assignment to new users without a blog on the main blog:

    Go to the same place (line 1103) in 'wpmu-functions.php' where it says

    // Newly created users have no roles or caps until they are added to a blog.
    update_user_option($user_id, 'capabilities', '');
    update_user_option($user_id, 'user_level', '');

    and fill in the empty single quotes with the desired default capabilities and user level, such as

    update_user_option($user_id, 'capabilities', 'a:1:{s:6:"author";b:1;}');
    update_user_option($user_id, 'user_level', '2');

    Newly registered users will then have author priviledges on the main blog. It seems to work... what do you think?

  13. andrea_r
    Moderator
    Posted 7 years ago #

    dimonprodigy, you shoudl probably ask your question in a new thread, otherwise it gets missed (as you've seen).

    Also, has anyone bothered to submit this (users with no blog have no role anywhere and can't get blog) to trac?

  14. LynneLiL
    Member
    Posted 7 years ago #

    As a newbie installing MU (hi andrea) I have to deal with this problem before I move forward...I want to default users to being Subscribers (Level_0). Obviously I can use this code above, but what would change for the variables:

    update_user_option($user_id, 'capabilities', 'a:1:{s:6:"subscriber";b:1;}');
    update_user_option($user_id, 'user_level', '0');

    Would that work? What does the a:1/s:6/b:1 code in that bit stand for? I have no idea how to look that up in the forum if it's elsewhere. :)

    And does this default users to be able to subscribe to the main blog, but not any other blogs in the system?

    (BTW andrea your help was greatly appreciated in the other thread...I may decide to go with something other than MU for my personal community blog, but MU is PERFECT for another project I decided to take on, so it's all good.)

    Oh, and as an aside, how annoying is it to be testing the system, create yourself a new username "with no blog", and then be STUCK being logged in as that user, and any attempts to log out (or rather, get to the login screen somehow to log in as the admin) it gives you that "no permissions" page?? I was about to have a conniption fit, luckily I have another test version of MU elsewhere so I could see what the logout URL is (/wp-login.php?action=logout). That No Permissions page should have a stinkin' logout link so if you accidently get into using a username without permissions, you can log out!!

  15. LynneLiL
    Member
    Posted 7 years ago #

    I should add:

    I will have multiple sub blogs. The main blog will be an RSS congolmeration of latest posts from those blogs, for the most part (with maybe some interim announcements from the main blog, from me). I will be disabling letting anyone just add a blog, it's for very specific people (most of whom have no idea how to really blog) whom I will be signing up for the blog specifically.

    But, I don't know if it's a good idea to disable joining the main blog/aubblogs by readers. Other than being logged in to comment (though I probably will leave comments open to non-logged-in users) I don't know why there should be a link to add new user at all.

    However, I do not want any user to go through what I just went through, not being able to move around to log out.

  16. andrea_r
    Moderator
    Posted 7 years ago #

    (Hi, and you're welcome. :) )

    I have a hack somewhere that might help... aha! Found it.

    OPEN:

    wp-includes/registration.php

    FIND:

    function wp_insert_user($userdata) {

    FURTHER DOWN, FIND:

    if ( $update )
    do_action('profile_update', $user_id);
    else
    do_action('user_register', $user_id);

    AFTER, ADD:

    add_user_to_blog('X', $user_id, 'XXroleXX');

    Where:

    X = The Blog ID you want to add the user to
    XXroleXX = The role for the user

    Note, was not me who figurred that out. I can't remember if this has been addressed in the nightly builds or not. (Friggin' meds...)

  17. lunabyte
    Member
    Posted 7 years ago #

    Has this issue been addressed in Trac yet?

    I didn't see it in any of the open/closed tickets.

    IMHO, "just a username" should at least give them subscriber/0 access on the main blog so they can change their password, etc.

    Either that, or make some sort of check where if the users doesn't have a blog, there is some way for them to change their info.

  18. andrea_r
    Moderator
    Posted 7 years ago #

    It's been a long-term problem, I know that. (off to browse trac)

  19. lunabyte
    Member
    Posted 7 years ago #

    I think what would make the "most" sense is the register link accepting a parameter where it know what "site" they came from, and make that their "default" site. But, at this point, just logging in and using the main site for subscriber/0 permissions would be better than nothing.

  20. LynneLiL
    Member
    Posted 7 years ago #

    Thanks muchly, all, I think this works for me. Where we'll be restricting all new users to ONLY regular usage, NOT creating new blogs (I guess that means hacking signup.php to not include that option?) and creating blogs FOR certain people who come to us (it's the best option for the particular usage we will have) having all new users automatically get added to the main blog as subscribers at least gets them in the system

    I'm partway into deployment for this new project, and am impressed so far with the ease that users (blog owners or just subscribers to one) can log in on one blog and then comment and whatnot on other blogs with that same username. It's perfect for what we want to deploy.

    And as for my other (leftinlowell.com) project, I've emailed my friend who's a expert WP user, and she has some ideas for how to use WP2.1 regular software to deploy what I want for my community site.

    It's too bad I can't put as much enthusiasm and time into doing actual paid work instead of this volunteer stuff. :)

  21. hempsworth
    Member
    Posted 7 years ago #

    andrea_r described a hack erlier:

    OPEN:

    wp-includes/registration.php

    FIND:

    function wp_insert_user($userdata) {

    FURTHER DOWN, FIND:

    if ( $update )
    do_action('profile_update', $user_id);
    else
    do_action('user_register', $user_id);

    AFTER, ADD:

    add_user_to_blog('X', $user_id, 'XXroleXX');

    Where:

    X = The Blog ID you want to add the user to
    XXroleXX = The role for the user

    What would X and XXroleXX be if I wanted to add them to the MAIN BLOG as SUBSCRIBERS?

    Thanks

  22. nicoa
    Member
    Posted 6 years ago #

    i have written a small plugin...
    see http://mu.wordpress.org/forums/topic.php?id=4166&replies=1

    servus,
    nico

  23. Megalion
    Member
    Posted 6 years ago #

    If you're wanting to use merqtio's code examples but need info about what user level is what. Check out this page:
    http://codex.wordpress.org/Roles_and_Capabilities

    I'm trying to figure it out too and looks like that page might shine a lot of light on it if not have the answers you're looking for.

  24. enterpriseJM
    Member
    Posted 6 years ago #

    "X = The Blog ID you want to add the user to
    XXroleXX = The role for the user"

    I've been struggling with this for more than an hour now. It would be so helpful if, when providing information to edit PHP files with, you would use conventional and accurate examples. You may think my comments an affront to you and the seemingly tireless support you offer, but I see so many others suffering the frustrations of trying to get this right - not just this particular modification, but all the other major and minor edits we try to do in order to make our blogs better.

    "X = The Blog ID" was easy. X always equals the alpha-numeric character that would represent, in this case a number (well, actually, the character is usually represented by a lowercase "x", but at least this was easy to understand).

    "XXroleXX = The role for the user" is the mystery. I have searched all my WP-MU files and everywhere I could think of online and the codex. Nowhere is there any representation of 2 sets of alpha-numeric characters flanking the word "role". If this was meant to be "level_xx", then that's the way it should be represented. If it was meant to represent some other term, it would have been better to represent it in a way that makes it more clear. If it could be any one or more of several terms, it might have been most helpful to use an example to give us an idea what and how the term is used.

    I'm trying hard to be more or less self-sufficient here. I have asked a couple of questions so far on WordPress forums - worded simply, succinctly and politely - and have gotten zero response from anyone. It's frustrating. It reminds me of 16 years ago when I got my first computer and sought help on CompuServe forums. Everyone spoke a foreign language called technogeek. I evolved into a support specialist who became quite popular because I spoke less geekish and adhered to the conventions when giving examples.

    I know we are supposed to be very familiar with servers and coding, but the reality is, most of us know enough to get into deep trouble. I have no doubt most of the regulars here know that. Please don't punish us for wanting to learn right from wrong.

    Thanks for letting me complain.

  25. andrea_r
    Moderator
    Posted 6 years ago #

    I'm trying to figure out if the second paragraph was directed to me or not.

    In the code example, I can heartily agree, the "xxrolexx" was incorrect. I didn't write it though, I noted I grabbed it from elesewhere. If you note at the bottom there's an aside about meds I was on at the time - which was from some pretty major surgery. Sitting in front of the computer was one of the very few things I coudl do at that time. (note the dates on the thread)

    So, sometimes we don't give perfect examples in perfect conditions. :)

    A quick check with the code shows that the examples wasn't in that format, as you discovered, and at such times you should go with what's in the code and not in an outdated thread.

    Friendly tone here, I sure do know how frustrating it is learning all this stuff.

  26. wep
    Member
    Posted 6 years ago #

    I no understand, user level by default is 0, but redirect to wp-admin/profile and is BUG or not?, my trouble is with version 1.2.3, i want change user level..,

  27. gemanshi
    Member
    Posted 5 years ago #

    Could someone please explain what the a:1/s:6/b:1 code in that bit written by JoshEH stand for? I'm attempting to make the default role be 'author'.

  28. cafespain
    Member
    Posted 5 years ago #

    In short it's a serialised array or object - I'm refusing to use the Americanism :) See :
    http://es.php.net/manual/en/language.oop.serialization.php

    The easiest way to find out what you should set it to, is to create a user in your system - set their role as Author then have a look at the serialised values created in the database for that user and set it in your script accordingly.

  29. gemanshi
    Member
    Posted 5 years ago #

    Thanks!

  30. gemanshi
    Member
    Posted 5 years ago #

    I think I may have spoke to soon in regards to figuring this out. I set up a user as an author per the above suggestion. My only problem now is that I've been unable to find the serialized values in the database. Where exactly would they be?

About this Topic