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™

Changing Roles/Capabilities via PHP

Common Questions/Problems/Tips. Posted by Administrators & Support Reps.

Changing Roles/Capabilities via PHP

Postby Jason Caldwell » March 4th, 2011, 10:45 pm

Quick example of a Role change:
Code: Select all
<?php
$user_id 
= "123";
$user = new WP_User($user_id);
$user->set_role("s2member_level1");
?>

Where "s2member_level1", could be any Role that you've configured in your WordPress installation.

Here are the most common Roles:
subscriber
s2member_level1
s2member_level2
s2member_level3
s2member_level4

author
contributor
administrator
editor

Quick example of adding/removing individual Capabilities:
Code: Select all
<?php
$user_id 
= "123";
$user = new WP_User($user_id);
$user->add_cap("access_s2member_level0");
$user->add_cap("access_s2member_level1");
$user->add_cap("access_s2member_ccap_music");
$user->add_cap("access_s2member_ccap_videos");
$user->remove_cap("access_s2member_level2");
?>
~ Jason Caldwell / Lead Developer
& Zeitgeist Movie Advocate: http://www.zeitgeistmovie.com/

Is the s2Member plugin working for you? Please rate s2Member at WordPress.org.
You'll need a WordPress.org account ( comes in handy ). Then rate s2Member here Image
.
User avatar
Jason Caldwell
Lead Developer
Lead Developer
 
Posts: 4045
Joined: May 3, 2010
Location: Georgia / USA

Re: Changing Roles/Capabilities via PHP

Postby Jason Caldwell » March 15th, 2011, 1:06 am

If you wanted to loop through all of your Users in a CRON job
or in some other custom coding project, you might do something like this.

Create this directory and file:
/wp-content/mu-plugins/my-cron-job.php

Inside the file, do something like this:
Code: Select all
<?php
add_action 
("init", "my_cron_job", 1);
function my_cron_job ()
    {
        if ($_GET["my_cron_job"] === "secret-cron-job-key")
            {
                foreach (get_users () as $user)
                    {
                        $user = new WP_User ($user->ID);
                        $_10_days_ago = strtotime ("-10 days");
                        if (s2member_registration_time ($user->ID) <= $_10_days_ago)
                            {
                                # A Member for at least 10 days.
                                # Promote them now to Level #2.
                                if (!$user->has_cap ("access_s2member_level2"))
                                    $user->set_role ("s2member_level2");
                            }
                    }
                /**/
                exit ();
            }
    }
?>
* Now create a CRON job that calls this URL periodically ( once a day perhaps ).
Code: Select all
http://www.example.com/?my_cron_job=secret-cron-job-key
~ Jason Caldwell / Lead Developer
& Zeitgeist Movie Advocate: http://www.zeitgeistmovie.com/

Is the s2Member plugin working for you? Please rate s2Member at WordPress.org.
You'll need a WordPress.org account ( comes in handy ). Then rate s2Member here Image
.
User avatar
Jason Caldwell
Lead Developer
Lead Developer
 
Posts: 4045
Joined: May 3, 2010
Location: Georgia / USA

Re: Changing Roles/Capabilities via PHP

Postby davidpayne » November 21st, 2011, 5:48 am

Jason,

Thanks for all your work on this plugin, it's really great!

What I would like to do is have buttons available to users which upon clicking will set for them a custom capability. Let me explain. My community is about Physician Assistant education. My users generally fall into one of four categories: prospective physician assistant, physician assistant student, physician assistant educator, and clinical physician assistant. I would like to create four small buttons that upon being clicked would add to the user a custom capability. For example, a prospective physician assistant would click on a button and would then have the custom capability "prospectivepa" given to them. Once they landed a seat in PA school and became a student then could then "change their status" by clicking on a button which would remove the "prospectivepa" capability and give them the "pastudent" custom capability. Does this make sense?

Is something like this possible? I understand that this might take some work and would be happy to provide a donation for your efforts!

David Payne
User avatar
davidpayne
Registered User
Registered User
 
Posts: 9
Joined: July 7, 2011

Re: Changing Roles/Capabilities via PHP

Postby Jason Caldwell » November 21st, 2011, 1:56 pm

Thanks for the follow-up.

Yes, this can certainly be accomplished. Based on the example I gave earlier, you would do something like this in your custom PHP code. You might seek assistance from s2Installs.com, or on jobs.wordpress.net if you feel it's required in this case. You might point them to this thread even.

Add Custom Capability.
Code: Select all
<?php
$user_id 
= 123;
$user = new WP_User($user_id);
/* Or, $user = wp_get_current_user(); */
$user->add_cap("access_s2member_ccap_prospectivepa");
?>

Remove Custom Capability and add another.
Code: Select all
<?php
$user_id 
= 123;
$user = new WP_User($user_id);
/* Or, $user = wp_get_current_user(); */
$user->remove_cap("access_s2member_ccap_prospectivepa");
$user->add_cap("access_s2member_ccap_pastudent");
?>

Also, it is sometimes useful to loop through all existing Capabilities that User has.
Code: Select all
<?php
$user_id 
= 123;
$user = new WP_User($user_id);
/* Or, $user = wp_get_current_user(); */
foreach($user->allcaps as $cap)
   if(strpos($cap, "access_s2member_ccap_") === 0)
       echo 'This is a Custom Capability';
?>

s2Member® / Hot Tip: ( s2Installs.com! )

Recommended by s2Member® Lead Developer (Jason Caldwell). Their rate for a standard installation is $125. They're highly trained. Just request their service!

Need Help? Post A New Job!

It's free. Your Job will appear here, and @ jobs.wordpress.net. It will be displayed for a period of 21 days; or until you take it off, whichever comes first. Good luck!
~ Jason Caldwell / Lead Developer
& Zeitgeist Movie Advocate: http://www.zeitgeistmovie.com/

Is the s2Member plugin working for you? Please rate s2Member at WordPress.org.
You'll need a WordPress.org account ( comes in handy ). Then rate s2Member here Image
.
User avatar
Jason Caldwell
Lead Developer
Lead Developer
 
Posts: 4045
Joined: May 3, 2010
Location: Georgia / USA

Re: Changing Roles/Capabilities via PHP

Postby tonyt » January 16th, 2012, 8:50 am

This post has been really useful for me, but i was wondering if i could get a little help with a modification to the code.

At current i've made a plugin to run the following:

What i'm trying to achieve is a bit of automation like the cron job script, however the variable i need is to cross check a field.

Code: Select all
    <?php
    add_action ("init", "my_cron_job", 1);
    function my_cron_job ()
        {
            if ($_GET["my_cron_job"] === "secret-cron-job-key")
                {
                    foreach (get_users () as $user)
                        {
                            $user = new WP_User ($user->ID);
                            if (SELECT * FROM 'wp_users' WHERE training = 1)
                                {
                                    # A Member for at least 10 days.
                                    # Promote them now to Level #2.
                                    if (!$user->has_cap ("access_s2member_level2"))
                                        $user->set_role ("s2member_level2");
                                }
                        }
                    /**/
                    exit ();
                }
        }
    ?>


The difference between your code which checks registration. I have setup a field for 'training' inside wp_users. If the value of training = 1 (updated through a different script) then the roles need to be updated to level 2. Would you have any idea how this would be achieved?
User avatar
tonyt
Registered User
Registered User
 
Posts: 1
Joined: January 13, 2012


Return to Common Questions/Problems/Tips

Who is online

Users browsing this forum: No registered users and 1 guest

cron