What Action Hook should I use for ccaps add/change/delete?

I am working on an integration between s2Member and courses I have for sale. I have s2Member Pro and currently use “buy now” forms to sell my courses and set a ccaps field when a member purchases a course. The issue is that we then have to manually assign the course to the member. I have my auto-enroll php code written and a table to tie ccaps to my course ID. Now I need to trigger the action to assign the course to the member when there is a change to a member’s ccaps field. I have been searching for two days and cannot find a hook for this. Is there something you would recommend that will fire whenever ccaps changes (added via purchase, added or removed by admin on the “Edit User” screen and potentially added/changed/removed systematically when an EOT for the course is reached? Thanks for your help!

Try ‘profile_update’ and ‘user_register’. Also try the closets hook after the $user->add_cap() and $user->remove_cap() in the s2M code.

Update and thank you - I was pulled to a priority project so this sat for a while. However, now that I am back I wanted to let you know that the two hooks you suggested, ‘profile_update’ and ‘user_register’ are working great! I don’t need any others as it appears any change to the ccap field, whether made programmatically or manually, triggers the ‘profile_update’ action hook and my auto-enroll is working great! Thank you so much for your help with this.

Looks like I updated this too soon. Everything works perfectly when the admin changes the ccap on the profile or a new user is registered or the membership level is changed. However, when s2member updates the ccap based on a purchase, my code is not being triggered. I need another action hook, likely what you suggested, the closest hook after the $user->add_cap() and $user->remove_cap() in the s2M code. So how do I go about finding those hooks? (Sorry, I am new to WordPress.)

Just FYI - I have already tried:

ws_plugin__s2member_after_config_roles
ws_plugin__s2member_after_handle_profile_modifications
ws_plugin__s2member_during_paypal_notify_during_new_ccaps
added_user_meta
updated_user_meta

None of these are working - though they seem like they should be correct.

OK, I figured it out and now feel a bit dumb - but the issue was that I was not retrieving the User ID correctly for a hook to s2member. So now the action hook to ws_plugin__s2member_during_paypal_notify_during_new_ccaps works great - I just needed to add a function to retrieve the User ID as s2member was passing it then send it to my update function. A “Doh!” moment - but wanted to share in case anyone else has this same issue.

add_action(‘ws_plugin__s2member_during_paypal_notify_during_subscr_signup_wo_update_vars’, ‘s2PurchaseCourseAccess’);

function s2PurchaseCourseAccess ( $vars = array() ) {
$user_id = $vars[“user_id”];
$this->updateUserCourseAccess($user_id);
}

Good job, @CharlieGirl ! :slight_smile:

I have been searching for something just like this, but I am a beginner and I don’t quite understand “exactly” what I need to do to add this functionality to my site.
I’d like to be able to enroll a member into one of my Learndash courses whenever the ccap is added.
Any chance you could give me a little more detail on the how/what to do? Or point me in the right direction??
Thanks in advance!!

I don’t know or use Learndash as my LMS so I can’t really help there. I am using WP Courseware. But I can take you through the steps I followed. I created a table that simply lists my chosen ccap and then the course ID that goes with that ccap. I then wrote a php program that accepts a passed in user ID and retrieves the ccaps assigned to that user into an array. It reads the table to get the course assigned to each ccap from the array, then passes the user ID and the course ID to the WP Courseware program that assigns users to courses. My posting here was to determine how to automatically kick off that bridge program I wrote when a course is purchased through S2 Member. My program is written as a WordPress Plugin, making use of the Action Hooks that S2Member has which allow for that automation. So, not knowing anything about your level of programming expertise, I would point you in the following direction: 1) You need to understand how to write a WordPress Plugin 2) You should look at the code for the Learndash plugin to find out how it is enrolling students and what you need to pass to it and how you can call it so it can perform that function (if it does enroll students already) 3) Read S2Members excellent documentation on Action Hooks and review their knowledge base: https://s2member.com/kb-article/hacking-s2member-plugin-w-hooksfilters-for-wordpress/

The two profile update and the one purchase action hooks I describe above worked for me using the PayPal payment platform. What you need may be different depending on how you are receiving payment for and managing your classes.

By the way - in the year since I created my plugin, WP Courseware updated their software to do this automatically for me - so no customization is required now.

Thank you so much for the input!
I will see how far I can get on my meager programming skills…
Learndash has a nice little hook for enrollment: ld_update_course_access($user_id, $course_id, $remove = false);
I was just going to make the ccap the course_id number to keep it really simple.
But now you have me thinking I should get my wordpress skills up a notch and learn to make plugins! Might come handy again and again.
Feeling inspired.

Hi MBSM

I used gravity forms to achieve something like this.

you can add capps using some code I found here

https://legacy.forums.gravityhelp.com/topic/adding-custom-capability-issues

Add the code to the function php in your child theme. Then install the integration for learndash and gravity forms which is free.

Then you can make a sign up form on the site and link it to a ccap and a learndash course. Also you can integrate with woocommerce product so selling the course is also easier.

I hope this helps