Skip to content

Instantly share code, notes, and snippets.

@andrewlimaza
Created October 19, 2020 12:20
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 andrewlimaza/732fec20eb7275c6a74cd57a7efef495 to your computer and use it in GitHub Desktop.
Save andrewlimaza/732fec20eb7275c6a74cd57a7efef495 to your computer and use it in GitHub Desktop.
Hide membership shortcode content for non-members/logged-out users when using negative levels.
<?php
/**
* Hide content for logged-out and user's without levels when using [membership level="-20"] shortcode.
* This will only hide content if negative levels are used. Default functionality remains if using "10,-20" in the level's attribute.
* Follow this guide to add this code to your site - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_custom_shortcode_check( $hasaccess, $content, $levels, $delay ) {
// Check if array contains only negative numbers.
$negative = false;
foreach( $levels as $level ) {
if ( $level < 0 ) {
$negative = true;
} else {
$negative = false;
}
}
if ( $negative && ( ! is_user_logged_in() || ! pmpro_hasMembershipLevel() ) ) {
$hasaccess = false;
}
return $hasaccess;
}
add_filter( 'pmpro_member_shortcode_access', 'my_custom_shortcode_check', 10, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment