Created
October 19, 2020 12:20
-
-
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.
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 | |
/** | |
* 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