Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save andrewlimaza/429c35da45b33615d0cff3a34f211b11 to your computer and use it in GitHub Desktop.
Save andrewlimaza/429c35da45b33615d0cff3a34f211b11 to your computer and use it in GitHub Desktop.
Give non-members or visitors access to posts/shortcodes/blocks for specific post ID's
<?php
/**
* Bypasses all content restriction for posts, pages, blocks and shortcodes that are on certain pages.
* Tweak this code further for your needs.
* To add this code to your site, please follow this guide - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmpro_allowed_posts() {
$open_post_ids = array( 118 ); // Add all the POST ID's that you want to bypass here.
return $open_post_ids;
}
// Give access to the post if the visitor/non-member is trying to access a post that is in the list of allowed posts.
function my_pmpro_has_membership_access_filter( $hasaccess, $post, $user, $levels ) {
// already has access.
if ( $hasaccess ) {
return $hasaccess;
}
// if the user has access, return true
if ( in_array( $post->ID, my_pmpro_allowed_posts() ) ) {
$hasaccess = true;
}
return $hasaccess;
}
add_filter( 'pmpro_has_membership_access_filter', 'my_pmpro_has_membership_access_filter', 10, 4 );
// Give the person a 'fake' level to bypass shortcode and block content as well.
function my_pmpro_has_membership_level( $haslevel, $user_id, $levels ) {
global $post;
// Already has a membership level, let's just bail.
if ( $haslevel ) {
return $haslevel;
}
if ( in_array( $post->ID, my_pmpro_allowed_posts() ) ) {
$haslevel = true;
}
return $haslevel;
}
add_filter( 'pmpro_has_membership_level', 'my_pmpro_has_membership_level', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment