Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save dwanjuki/a5a59bde2a667d77073ffdb84d634b14 to your computer and use it in GitHub Desktop.

Select an option

Save dwanjuki/a5a59bde2a667d77073ffdb84d634b14 to your computer and use it in GitHub Desktop.
Restrict access to Custom Post Type based on membership level.
<?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