Page 1 of 1

Demoting users with PHP

PostPosted: August 10th, 2011, 8:11 am
by randomskate1
I am trying this and I cannot get it to work:
Code: Select all
$demote_to = S2MEMBER_CURRENT_USER_ACCESS_LEVEL – $demotion_value;
(works fine)
but THEN…this:

Code: Select all
$demotion_role = c_ws_plugin__s2member_option_forces::force_demotion_role ($demote_to);


or this:
Code: Select all
$demotion_role = c_ws_plugin__s2member_option_forces::force_demotion_role ( ‘s2Member Level ‘ . $demote_to);


doesn’t work. I cannot find much documentation anywhere about this function either, It will refuse to demote the member, any help will be appreciated a lot. Thanks!

Re: Demoting users with PHP

PostPosted: August 10th, 2011, 12:11 pm
by Cristián Lávaque
What do you want to demote to? Free subscriber? You could use set_role, something like:

Code: Select all
$user = new WP_User($user_id);
$user->set_role('subscriber');
 

Re: Demoting users with PHP

PostPosted: August 10th, 2011, 5:41 pm
by randomskate1
thanks, will this work with the pro membership to demote to level 88 for example?

Re: Demoting users with PHP

PostPosted: August 10th, 2011, 6:13 pm
by Cristián Lávaque
It should, as far as I can tell, just use the right role name.

Re: Demoting users with PHP

PostPosted: August 10th, 2011, 7:21 pm
by randomskate1
Thanks for the help but I cannot get it to work still, It simply just demotes the user back down to level 0 which is not what I want, even if you specify a level..I have tried the following without any success... Maybe I'm doing it wrong:

Code: Select all
$user = new WP_User(bp_current_user_id());
$user->set_role(81);


Code: Select all
$user = new WP_User(bp_current_user_id());
$user->set_role('level_81');


Code: Select all
$user = new WP_User(bp_current_user_id());
$user->set_role('s2Member Level 81');


Code: Select all
$user = new WP_User(bp_current_user_id());
$user->set_role('s2MemberLevel81');


... These are just some of the things I've tried without any success. Cheers for the help

Re: Demoting users with PHP

PostPosted: August 10th, 2011, 7:26 pm
by Cristián Lávaque
's2member_level81' seems to be the right one. Does bp_current_user_id() give you the WordPress user ID?

Re: Demoting users with PHP

PostPosted: August 10th, 2011, 7:34 pm
by Cristián Lávaque
Nope, just looked it up and it doesn't.

You need to get the user's ID in WordPress, it's a unique number for that user. It may be obviously available in the code where you're adding this customization, or you can study these WordPress functions to find a way to get it https://codex.wordpress.org/Function_Re ... _Functions

Re: Demoting users with PHP

PostPosted: August 10th, 2011, 7:59 pm
by randomskate1
Thanks for editing my posts! I'll add the code tags this time, sorry. hmm. still not working... Both of these versions seem to return the proper user ID

I am calling the set_role function from within messages_screen_compose() in bp-messages.php under the condition that the message successfully sends, but in either case, the user gets demoted to level 0 instead of level 81.

Code: Select all
$current_user = wp_get_current_user();
$user = new WP_User($current_user->ID);
$user->set_role('s2Member Level 81');


Code: Select all
$user = new WP_User(bp_current_user_id());
$user->set_role('s2Member Level 81');


Thanks!

Re: Demoting users with PHP

PostPosted: August 10th, 2011, 8:19 pm
by randomskate1
should i use mysql_query to modify the database directly? i would of liked to avoid it and would think you don't need to do it this way with s2m. If I did however, would there be any potential issues from doing it like this?

Cheers

Re: Demoting users with PHP

PostPosted: August 10th, 2011, 9:03 pm
by Bruce C
Try using
Code: Select all
get_current_user_id()


instead of
Code: Select all
wp_get_current_user()

Re: Demoting users with PHP

PostPosted: August 10th, 2011, 9:44 pm
by randomskate1
Thanks for the suggestion, I tried it but still get the same result. Getting the user ID is not the problem, that part definately works fine. Problem is that the user with whatever ID just gets demoted to level 0 instead of the desired level.

With a closer look into the database in the wp_usermeta table, I found that "s2member_levelXX" seems to be the proper convention (from my understanding), but this still doesn't work, just demotes to 0. setting "subscriber" works but this is not what i want... is there an alternative for the set_role() function ?

Re: Demoting users with PHP

PostPosted: August 10th, 2011, 10:15 pm
by Cristián Lávaque
Try this: create a test user, set it to Level3, then note his ID number, then try the code. Supposing the user's ID were 21, then it'd be:

Code: Select all
$user = new WP_User(21);
$user->set_role('s2member_level2'); 


Go to his profile and see if the role got updated.

Re: Demoting users with PHP

PostPosted: August 11th, 2011, 1:17 am
by randomskate1
works. I'm an idiot. I forgot to make $demote_to global.

correct syntax is: \
Code: Select all
$user->set_role( 's2member_level'. $demote_to );


thanks for the help!

Re: Demoting users with PHP

PostPosted: August 11th, 2011, 1:21 am
by Cristián Lávaque
Great. :)