Skip to content

Instantly share code, notes, and snippets.

@verygoodplugins
Last active December 13, 2018 17:20
Show Gist options
  • Save verygoodplugins/8b9e58c2ee9b44752f48aece180dcdbc to your computer and use it in GitHub Desktop.
Save verygoodplugins/8b9e58c2ee9b44752f48aece180dcdbc to your computer and use it in GitHub Desktop.
Flips the access rules for a post or posts so that users *without* any of the specified tags are granted access
<?php
add_filter('wpf_user_can_access', 'my_wpf_flip_access_rules', 10, 3);
function my_wpf_flip_access_rules( $can_access, $user_id, $post_id ) {
// Optional: limit the rule flipping just to certain post IDs (1234 and 4567)
if( $post_id != 1234 ) {
return $can_access;
}
$post_restrictions = get_post_meta( $post_id, 'wpf-settings', true );
if(empty($post_restrictions) || empty($post_restrictions['allow_tags'])) {
return $can_access;
}
$user_tags = wp_fusion()->user->get_tags( $user_id );
$result = array_intersect($user_tags, $post_restrictions['allow_tags']);
if( empty($result) ) {
// If user does *not* have any of the tags specified in the post restrictions, grant access
return true;
} else {
// If user *does* have any of the tags, deny access
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment