Last active
April 7, 2025 08:51
-
-
Save ipokkel/54d59f7a4033060d0d35d7baceca7b57 to your computer and use it in GitHub Desktop.
Restrict all posts for a custom post type (CPT) automatically with PMPro but allow administrators access to the CPT #cpt #pmpro-cpt
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 | |
/** | |
* This recipe restricts all content of a Custom Post Type (CPT) | |
* to members only. | |
* | |
* This recipe assumes your CPT name is "recipes". | |
* You should replace this with your CPT name for the filter name | |
* and the $my_cpt variable's value in the hook name on line 71, | |
* e.g. pmpro_has_membership_access_filter_cpt-name, | |
* and 74, e.g. $my_cpt = 'cpt-name'. | |
* | |
* MEMBERSHIP LEVEL ACCESS: | |
* By default, this recipe gives all membership levels access to the CPT content. | |
* To restrict access to specific membership levels only: | |
* 1. Find your level IDs in Memberships > Settings > Membership Levels | |
* (hover over a level and check the URL for &level=X where X is the ID) | |
* 2. Modify line 47 to specify which level IDs should have access, for example: | |
* $my_post_membership_levels_ids = array(1, 2); // Only levels 1 & 2 get access | |
* | |
* 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_restrict_all_cpt( $hasaccess, $thepost, $theuser, $post_membership_levels ) { | |
// If PMPro says false already, return false. | |
if ( ! $hasaccess ) { | |
// Give admin access | |
if ( current_user_can( 'manage_options' ) ) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
if ( ! is_user_logged_in() ) { | |
return false; | |
} | |
// Get levels | |
$pmpro_levels = pmpro_getAllLevels( true ); | |
$post_membership_levels_ids = array_keys( $pmpro_levels ); | |
// Set levels to restrict access to CPT for specific levels only (Optional) | |
$my_post_membership_levels_ids = array(); // set specific levels here, e.g. array( 1, 2, 3, 4 ); | |
if ( ! empty( $my_post_membership_levels_ids ) ) { | |
$post_membership_levels_ids = $my_post_membership_levels_ids; | |
} | |
$theuser->membership_levels = pmpro_getMembershipLevelsForUser( $theuser->ID ); | |
$mylevelids = array(); | |
foreach ( $theuser->membership_levels as $curlevel ) { | |
$mylevelids[] = $curlevel->id; | |
} | |
if ( count( $theuser->membership_levels ) > 0 && count( array_intersect( $mylevelids, $post_membership_levels_ids ) ) > 0 ) { | |
//the users membership id is one that will grant access | |
$hasaccess = true; | |
} else { | |
//user isn't a member of a level with access | |
$hasaccess = false; | |
} | |
return $hasaccess; | |
} | |
// set the filter name to your post type name: pmpro_has_membership_access_filter_[post_type] | |
add_filter( 'pmpro_has_membership_access_filter_recipes', 'my_pmpro_restrict_all_cpt', 10, 4 ); | |
function disable_pmpro_cpt_redirect() { | |
$my_cpt = 'recipes'; // Set your custom post type name here | |
// check if post belongs to the CPT | |
if ( is_singular() && get_post_type() === $my_cpt ) { | |
if ( has_action( 'template_redirect', 'pmprocpt_template_redirect' ) ) { | |
// Disable the PMPro CPT redirect. | |
remove_action( 'template_redirect', 'pmprocpt_template_redirect' ); | |
} | |
} | |
} | |
add_action( 'wp', 'disable_pmpro_cpt_redirect' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With Paid Memberships Pro version 3.0+ this should be changed. Based on the #2666 PR perhaps:
$pmpro_levels = pmpro_getAllLevels();