Skip to content

Instantly share code, notes, and snippets.

@Pebblo
Created December 5, 2017 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pebblo/47ee6301aa5767066074b4a450309bb7 to your computer and use it in GitHub Desktop.
Save Pebblo/47ee6301aa5767066074b4a450309bb7 to your computer and use it in GitHub Desktop.
Example of how to add a capability to a user when using the WP User add-on. This function takes all of event's category slugs, loops over them and adds the capability 'access_s2member_ccap_{category_slug}' to the current user.
<?php //Please do not include the opening PHP tag if you already have one
function tw_add_custom_ccaps_on_registrations( $user, $attendee, $registration) {
//Check we have instances of the objects we need/expect.
if (! $user instanceof WP_User) {
continue;
}
if (! $attendee instanceof EE_Attendee) {
continue;
}
if (! $registration instanceof EE_Registration) {
continue;
}
$event = $registration->event();
if (! $event instanceof EE_Event ) {
continue;
}
//Pull the categories from the event
$categories = $event->get_all_event_categories();
//Loop over each category and add a ccap using the category slug
foreach($categories as $category) {
if ( $category instanceof EE_Term ) {
$user->add_cap( 'access_s2member_ccap_' . $category->slug() );
}
}
}
add_action('AHEE__EED_WP_Users_SPCO__process_wpuser_for_attendee__user_user_updated', 'tw_add_custom_ccaps_on_registrations', 10, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment