Created
January 10, 2022 22:51
-
-
Save sc0ttkclark/f561793c30855805c326990efc75707a to your computer and use it in GitHub Desktop.
Restrict access to specific tickets / RSVPs depending on Membership Level. Requires Event Tickets 5.2.3+
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Restrict access to specific tickets / RSVPs depending on Membership Level. | |
* | |
* Requires Event Tickets 5.2.3+ | |
* | |
* You can add this recipe to your site by creating a custom plugin | |
* or using the Code Snippets plugin available for free in the WordPress repository. | |
* Read this companion article for step-by-step directions on either method. | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
function my_pmpro_event_tickets_visibility( $return, $event_id, $ticket_id ) { | |
// Do not run in the admin area. | |
if ( is_admin() ) { | |
return $return; | |
} | |
/** | |
* Example 1: Ticket ID 63 will only be visible to those who have Membership Level 3. | |
* Example 2: Ticket ID 65 will only be visible to those who have Membership Level 1 or 2. | |
* | |
* You'll want to replace this example with your own ticket IDs and membership level IDs. | |
*/ | |
$protected_ticket_ids = [ | |
// Ticket ID => List of Membership Level IDs. | |
63 => [ | |
// Membership Level ID(s) to restrict the ticket to. | |
3, | |
], | |
65 => [ | |
// Membership Level ID(s) to restrict the ticket to. | |
1, | |
2, | |
], | |
]; | |
// If the ticket is protected, return false so that it does not load. | |
if ( isset( $protected_ticket_ids[ $ticket_id ] ) && ! pmpro_hasMembershipLevel( $protected_ticket_ids[ $ticket_id ], get_current_user_id() ) ) { | |
return null; | |
} | |
return $return; | |
} | |
add_filter( 'tribe_tickets_tpp_get_ticket', 'my_pmpro_event_tickets_visibility', 10, 3 ); | |
add_filter( 'tribe_tickets_rsvp_get_ticket', 'my_pmpro_event_tickets_visibility', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment