• Resolved SBlancher

    (@sblancher)


    Hello Hack Community!

    So I have a very odd scenario, but first let it be known I’m using the plugin Role Manager (found here). I realize this could entirely be the issue but I’ve tried deactivating it and running my code and it still does not work.

    Full Scenario: I’ve created a function (in functions.php) to fire after a customer buys a membership product. I need their role elevated after payment is received.

    So my problem is that when I am using the set_role function to change a users role to a custom one I’ve created, it doesn’t completely work. It actually sets the users role to ‘None’. Under the edit page of that user it further defines this as ‘No role for this site’. Even more odd it just happens to list the user under the custom role I created! Further testing proved that if I even use set_role to change the user role to ‘Administrator’, it once again sets the user role to ‘None’ but LISTS THE USER UNDER ADMINISTRATORS. Very peculiar. This means they are listed properly but have none of the capabilities of the role.

    Here is my usage of set_role in my function:
    global $current_user;
    get_currentuserinfo();
    $wp_user_object = new WP_User($current_user->ID); //not used
    $current_user->set_role(SisterhoodMember);

    I was using a WP_User object before but it was the same as using $current_user so I went with the more direct route. Can anyone help me out with this?? Is it the plugin? I really would like to keep it, my client has been using it for quite a while.

    If the full function is necessary I will post it, I’d rather not but I will if necessary. Thank you to anyone who can help me!!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Maybe the forum’s parser is responsible, you have a syntax error in set_role(), the passed parameter must be a string with quotes:
    $current_user->set_role('SisterhoodMember');

    That doesn’t fully explain the behavior you are seeing, and maybe your code does have quotes, in which case I don’t know what the problem is.

    To ensure we see the same code you are actually using, put your code between backticks so the forum’s parser doesn’t corrupt it.

    Thread Starter SBlancher

    (@sblancher)

    Right, a friend had pointed that out too. I have it properly in quotes in my code. Just for reference, here is the print_r of the user after set_role:

    object(WP_User)#411 (7) { [“data”]=> object(stdClass)#448 (12) { [“ID”]=> string(4) “2076” [“user_login”]=> string(7) “Gandalf” [“user_pass”]=> string(34) “SOMETHING” [“user_nicename”]=> string(7) “gandalf” [“user_email”]=> string(28) “SOMETHING” [“user_url”]=> string(0) “” [“user_registered”]=> string(19) “2014-10-24 02:58:19” [“user_activation_key”]=> string(0) “” [“user_status”]=> string(1) “0” [“display_name”]=> string(7) “Gandalf” [“completed_registration”]=> string(5) “false” [“user_level”]=> int(0) } [“ID”]=> int(2076) [“caps”]=> array(1) { [“SisterhoodMember”]=> bool(true) } [“cap_key”]=> string(15) “wp_capabilities” [“roles”]=> array(0) { } [“allcaps”]=> array(1) { [“SisterhoodMember”]=> bool(true) } [“filter”]=> NULL } WP_User Object ( [data] => stdClass Object ( [ID] => 2076 [user_login] => Gandalf [user_pass] => SOMETHING. [user_nicename] => gandalf [user_email] => SOMETHING [user_url] => [user_registered] => 2014-10-24 02:58:19 [user_activation_key] => [user_status] => 0 [display_name] => Gandalf [completed_registration] => false [user_level] => 0 ) [ID] => 2076 [caps] => Array ( [SisterhoodMember] => 1 ) [cap_key] => wp_capabilities [roles] => Array ( ) [allcaps] => Array ( [SisterhoodMember] => 1 ) [filter] => )

    I believe the wp_capabilities [roles] => Array( ) being empty is disturbing. But wp_capabilities don’t determine which role you are… it’s all so frustrating. Please help!!

    Moderator bcworkz

    (@bcworkz)

    I think I’ve figured it out. If you look at the Members Roles list table, you should see a Role Label column and a Role Name column, among other columns. Note that all role names are all lowercase. It is the name that must be passed in set_role(), all lowercase. Trying to pass mixed case causes the bizarre behavior you are observing. If you pass what ever is in the Role Name column for your role as all lower case, I think your code should work for you. As a guess, since I can’t know what name was assigned, this should work:
    $current_user->set_role('sisterhoodmember');

    Thread Starter SBlancher

    (@sblancher)

    Thanks, this worked!! Very grateful *bow* *bow*

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘User set_role setting role to 'None'’ is closed to new replies.