PriMoThemes — now s2Member® (official notice)

This is now a very OLD forum system. It's in READ-ONLY mode.
All community interaction now occurs at WP Sharks™. See: new forums @ WP Sharks™

Demoting users with PHP

s2Member Plugin. A Membership plugin for WordPress®.

Demoting users with PHP

Postby randomskate1 » August 10th, 2011, 8:11 am

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!
Last edited by Cristián Lávaque on August 10th, 2011, 6:14 pm, edited 1 time in total.
Reason: Improve code readability. http://www.primothemes.com/forums/viewtopic.php?f=36&t=2780
User avatar
randomskate1
Registered User
Registered User
 
Posts: 8
Joined: August 10, 2011

Re: Demoting users with PHP

Postby Cristián Lávaque » August 10th, 2011, 12:11 pm

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');
 
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Demoting users with PHP

Postby randomskate1 » August 10th, 2011, 5:41 pm

thanks, will this work with the pro membership to demote to level 88 for example?
User avatar
randomskate1
Registered User
Registered User
 
Posts: 8
Joined: August 10, 2011

Re: Demoting users with PHP

Postby Cristián Lávaque » August 10th, 2011, 6:13 pm

It should, as far as I can tell, just use the right role name.
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Demoting users with PHP

Postby randomskate1 » August 10th, 2011, 7:21 pm

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
Last edited by Cristián Lávaque on August 10th, 2011, 7:35 pm, edited 1 time in total.
Reason: Improve code readability. http://www.primothemes.com/forums/viewtopic.php?f=36&t=2780
User avatar
randomskate1
Registered User
Registered User
 
Posts: 8
Joined: August 10, 2011

Re: Demoting users with PHP

Postby Cristián Lávaque » August 10th, 2011, 7:26 pm

's2member_level81' seems to be the right one. Does bp_current_user_id() give you the WordPress user ID?
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Demoting users with PHP

Postby Cristián Lávaque » August 10th, 2011, 7:34 pm

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
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Demoting users with PHP

Postby randomskate1 » August 10th, 2011, 7:59 pm

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!
User avatar
randomskate1
Registered User
Registered User
 
Posts: 8
Joined: August 10, 2011

Re: Demoting users with PHP

Postby randomskate1 » August 10th, 2011, 8:19 pm

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
User avatar
randomskate1
Registered User
Registered User
 
Posts: 8
Joined: August 10, 2011

Re: Demoting users with PHP

Postby Bruce C » August 10th, 2011, 9:03 pm

Try using
Code: Select all
get_current_user_id()


instead of
Code: Select all
wp_get_current_user()
~Bruce ( a.k.a. Ace )

If you're interested in a Professional Installation, or Custom Coding Job, you can send your request here.

Proud Supporter of:
The Zeitgeist Movement
and Occupy Everything
User avatar
Bruce C
Experienced User
Experienced User
 
Posts: 337
Joined: July 20, 2011

Re: Demoting users with PHP

Postby randomskate1 » August 10th, 2011, 9:44 pm

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 ?
User avatar
randomskate1
Registered User
Registered User
 
Posts: 8
Joined: August 10, 2011

Re: Demoting users with PHP

Postby Cristián Lávaque » August 10th, 2011, 10:15 pm

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.
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Demoting users with PHP

Postby randomskate1 » August 11th, 2011, 1:17 am

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!
User avatar
randomskate1
Registered User
Registered User
 
Posts: 8
Joined: August 10, 2011

Re: Demoting users with PHP

Postby Cristián Lávaque » August 11th, 2011, 1:21 am

Great. :)
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010


Return to s2Member Plugin

Who is online

Users browsing this forum: No registered users and 1 guest

cron