Skip to content

Instantly share code, notes, and snippets.

@ipokkel
Last active March 28, 2024 16:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipokkel/54d59f7a4033060d0d35d7baceca7b57 to your computer and use it in GitHub Desktop.
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
<?php
/**
* This recipe restricts all content of a Custom Post Type (CPT)
* to members only.
*
* This recipe assumes your CPT name is "recipe".
* You should replace this with your CPT name for the filter name
* and the $my_cpt variable's value.
*
* 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
global $pmpro_levels;
$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_recipe', 'my_pmpro_restrict_all_cpt', 10, 4 );
function disable_pmpro_cpt_redirect() {
$my_cpt = 'recipe'; // 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' ) ) {
remove_action( 'template_redirect', 'pmprocpt_template_redirect' );
}
}
}
add_action( 'wp', 'disable_pmpro_cpt_redirect' );
@Rsmith-gs
Copy link

// Get levels
global $pmpro_levels;

With Paid Memberships Pro version 3.0+ this should be changed. Based on the #2666 PR perhaps:
$pmpro_levels = pmpro_getAllLevels();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment