Support » Developing with WordPress » wp_insert_user with specific role how to

  • Resolved axew3


    on fly if anybody …
    i’ve intended that

                  $userdata = array(
                   'user_login'       =>  'myname',
                   'user_pass'        =>  'mypass,
                   'user_email'       =>  ',
                   'user_registered'  =>  date_i18n( 'Y-m-d H:i:s', time() ),
                   'role'             =>  'editor'
              $user_id = wp_insert_user( $userdata );

    would create an user myname, with editor capability.
    Instead, the created user is a subscriber.
    I can query after insertion, to update the usermeta value about capabilities, but i would like to know if possible, what value should be passed for param role on the above, to have the resulting created user, as editor, without updating after the related usermeta?

Viewing 13 replies - 1 through 13 (of 13 total)
  • hi,

    give this a try:

    So you insert first and update after that..
    I’m not sure why the role isn’t working on insert.

    unfortunately, the linked above not answer … while

    I’m not sure why the role isn’t working on insert

    is the question. What would be the correct way to pass this param (if never possible), so you not have to update after the role?

    Moderator bcworkz


    Your code is missing a couple closing quotes. After correcting for that, it works properly on my site, adding a user assigned the Editor role. If you have trouble with this on your site, your theme or one of the plugins is creating a conflict or intentionally preventing role assignments. Do you have any security plugins running? They could be restricting the assignment of privileged roles to new users.

    Hello all, yes i’ve see the code was not correct, due to paste/edit on fly while posting, so i’ve see after it wasn’t correct, but the time to edit was elapsed …
    yes it work, but
    it result to you that the code insert with role editor the user?
    It return to me, ever, a subscriber.
    I so need to update after, with something like this:
    $wpdb->query("UPDATE wp_usermeta SET meta_value = 'a:1:{s:6:"editor";b:1;}' WHERE user_id = '$user_id' AND meta_key = 'wp_capabilities'");
    last time for a while i’ve try to see on class-wp-user.php what happen and if i’m wrong with something, but after i’ve interrupt not following at moment …
    You can confirm that in your test the user is inserted with role editor if slug editor is passed for the role parameter?

    • This reply was modified 2 years, 11 months ago by axew3.

    p.s i’ve re-read your answer just now and no, there is no security plugin where i test the code, and the theme is ever pure twentysixteen.
    But if you say that on your test it work, so i will try to re-test all things as soon to check further more …

    Moderator bcworkz


    Yeah, the inserted user has the editor role assigned immediately after running your code on my site and not doing anything else user related other than checking the user list table to confirm the role. Passing other roles has the expected effect of assigning the requested role, be it administrator, author, or any other valid role.

    Where have you inserted your code? When does it execute? If too early, roles have not yet been established. I ran your code from an admin page I use to test code snippets, so it’s similar to placing code on a page template as far as execution timing.

    i’ve stop code execution just after the function wp_insert_user() to check, and i’ve see at this time the role always set as subscriber on db, so, i’ve suppose no other function and nothing else can interfere, the result is this.
    from 1371
    until 1410

    p.s so i need to check further more and discover what wrong with this, maybe starting from scratch …

    • This reply was modified 2 years, 11 months ago by axew3.
    Moderator bcworkz


    Thanks for the code reference. Unfortunately, because I’ve no idea when w3_check_phpbb_profile_wpnu() is called, it doesn’t really tell me what I wanted to know.

    Have you tried the latest version of WP w3all phpBB? It was released 3 days ago. I suspect a plugin or theme conflict of some sort is the cause, something that is hooked into the insert user process. Before looking into that though, we can rule out a contextual issue by temporarily placing your snippet on one of your theme’s templates. Load the page that uses the modified template. If the user is inserted as an Editor this way, then we know there’s an issue with when/where the code executes. If the problem persists with template code as well, there’s definitely a conflict.

    You can narrow down the conflict by switching to one of the twenty* themes and disabling all plugins. Again, temporarily place the snippet on a theme template and load a related page. The user ought to be properly inserted in this configuration. Switch back to your normal theme. Check the user insertion again, using the same modified template. Start activating your plugins one by one. When the user insertion again has the wrong role, the last activated module is at fault.

    On a distantly related note, you might be interested in the “marks” URL parameter available for all trac repository links, which highlights lines of text. For example:

    What you did is perfectly fine and totally understandable. I just thought “marks” was pretty cool when I discovered it, so I’m simply passing the knowledge along 🙂

    going to re-check all as i can:
    in wp_w3all.php it is wrapped on wp_authenticate:

    // a phpBB user not logged into phpBB, WP login first time 
    add_action( 'wp_authenticate', array( 'WP_w3all_phpbb', 'w3_check_phpbb_profile_wpnu' ), 10, 1 );

    it try to check if the logging in user, exist with presented credentials into phpBB: if it is, and not exist into WP, the function insert the user in WP.
    So i’ve think like you think that somewhere the code change in some way the flow and the result on inserting user … the same action, is done into verify_phpbb_credentials
    main function that in this case add an user, if he present a valid phpBB cookie, and an user with same username not exist in WP.
    Since these two, are the unique where the problem can come out, than i will re-check what can be wrong: i suspect in case a more high level of conflict between calls that, despite the execution should happen in one or other case, may fires together at some point … but since the function insert an user if it not exist, i assume this can’t be possible, than i’m little confused … required a go little more deep as soon …

    About code marks, it is very useful! Thank you!

    ps after a caffè, i’ve realize i’ve not answer to one question above, about latest version installed:
    the funny answer: yes, because the messy code has been committed by me!!

    • This reply was modified 2 years, 11 months ago by axew3. Reason: it was a caffè!
    Moderator bcworkz


    Oh, you’re the plugin author! Evidence that I can miss significant details. You’d be well served to keep that in mind 😉 I don’t think it changes anything beyond suggesting you update. I did another quickie test on my site. I hooked wp_authenticate and arbitrarily inserted an editor role user on any login. No check for existing anything, just did the insert. The user with editor role is inserted as expected, no matter who logs in. Invalid user, subscriber, administrator, anything that fires the hook works. I confirmed the added user can login and can do editor like things. It really works! (I wasn’t too sure it would)

    Of note is I do not have phpBB installed. I would expect it to have its fingers into the authentication process. Since it’s clearly a prerequisite for your plugin, you will need to figure out a work around somehow. Still, it’s a good idea to verify that phpBB is the culprit. Revert to default everything, which is how you should initially be developing anyway (plus any prerequisite modules, but deactivate these as well for now). Make a test plugin that only adds a user on login using your logic. Adjust your code to compensate for phpBB not being activated. The user insertion should work in this configuration. Activate phpBB and retest. Leave your normal plugin deactivated, use the test plugin again. I suspect the role will now be reduced, confirming phpBB is altering role assignment somehow.

    If you have trouble figuring out a workaround, try inquiring with the phpBB devs. Unfortunately, I’ve exactly zero experience with phpBB and would be of little help.

    I’m wondering if there might be a different approach entirely, but it may not matter, the trouble could be in the user insertion and not when or what hook fires to do it. I suppose you could directly enter a data row to the user table without using the normal WP functions beyond $wpdb methods. There’s more to user insertion than that row. Be sure you also enter required user meta if you go this route. There might be even more to user insertion than user and user meta tables, IDK.

    Thank for the suggested procedure to debug 😉
    Around the point about phpBB interactions, there is nothing that can interfere, roles are considered as separated thing into the two CMS. The only time where roles/permissions are compared, and used in the way as above reported, is when an user is inserted in WP, and only if it already exist in phpBB.

    This topic can be considered resolved/closed, and i will try to check as soon what other can interfere, between fired functions on wp_w3all plugin.

    So i will follow here in answer when ready.
    Thank you.

    so this morning i’ve check what could be wrong and i’ve test out all things:
    it return all is order, even if i remove the code added about update: i mean, the user is inserted with correct role without any subsequent update
    Really confused about why while testing last night it was returning ever a subscriber, inserting user in WP.
    Perhaps, some ghost was on air … or i was near to be a ghost, last night.
    Not sure what more true between the two.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘wp_insert_user with specific role how to’ is closed to new replies.