Created
March 27, 2026 09:10
-
-
Save dwanjuki/a5a59bde2a667d77073ffdb84d634b14 to your computer and use it in GitHub Desktop.
Restrict access to Custom Post Type based on membership level.
This file contains hidden or 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 CPT based on membership level. | |
| * | |
| * Ref: https://www.paidmembershipspro.com/hook/pmpro_has_membership_access_filter_post_type/ | |
| * | |
| * 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_has_membership_access_filter_cpt( $hasaccess, $post, $user, $levels ) { | |
| // Level IDs of memberships required to access this CPT. | |
| $required_level_ids = array( 1, 2, 3 ); | |
| if ( ! pmpro_hasMembershipLevel( $required_level_ids ) ) { | |
| $hasaccess = false; | |
| } | |
| return $hasaccess; | |
| } | |
| add_filter( 'pmpro_has_membership_access_filter_cpt', 'my_pmpro_has_membership_access_filter_cpt', 10, 4 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment