Community Support Forums — WordPress® ( Users Helping Users ) — 2012-01-16T08:50:05-05:00 http://www.primothemes.com/forums/feed.php?f=36&t=2599 2012-01-16T08:50:05-05:00 http://www.primothemes.com/forums/viewtopic.php?t=2599&p=60660#p60660 <![CDATA[Re: Changing Roles/Capabilities via PHP]]>
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:
    <?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?

Statistics: Posted by tonyt — January 16th, 2012, 8:50 am


]]>
2011-11-21T13:56:40-05:00 http://www.primothemes.com/forums/viewtopic.php?t=2599&p=53649#p53649 <![CDATA[Re: Changing Roles/Capabilities via PHP]]> 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:
<?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:
<?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:
<?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!

Statistics: Posted by Jason Caldwell — November 21st, 2011, 1:56 pm


]]>
2011-11-21T05:48:45-05:00 http://www.primothemes.com/forums/viewtopic.php?t=2599&p=53608#p53608 <![CDATA[Re: Changing Roles/Capabilities via PHP]]>
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

Statistics: Posted by davidpayne — November 21st, 2011, 5:48 am


]]>
2011-03-15T01:06:18-05:00 http://www.primothemes.com/forums/viewtopic.php?t=2599&p=7842#p7842 <![CDATA[Re: Changing Roles/Capabilities via PHP]]> 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:
<?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:
http://www.example.com/?my_cron_job=secret-cron-job-key

Statistics: Posted by Jason Caldwell — March 15th, 2011, 1:06 am


]]>
2011-03-04T22:45:44-05:00 http://www.primothemes.com/forums/viewtopic.php?t=2599&p=7409#p7409 <![CDATA[Changing Roles/Capabilities via PHP]]> Quick example of a Role change:
Code:
<?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:
<?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");
?>

Statistics: Posted by Jason Caldwell — March 4th, 2011, 10:45 pm


]]>