Created
May 13, 2022 09:12
-
-
Save wp-user-manager/967a21fc2df6e7eee6d2c17201e0db96 to your computer and use it in GitHub Desktop.
WP User Manager - Change the content restriction logic for users AND groups to users OR groups
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 | |
add_filter( 'wpumcr_pre_post_restriction', function ( $is_restricted, $post ) { | |
$posts = array( 684, 685 ); // CHANGE THIS to the posts you want to restrict with the OR logic | |
if ( ! in_array( $post->ID, $posts ) ) { | |
return $is_restricted; | |
} | |
$user_logged_in = is_user_logged_in(); | |
if ( ! $user_logged_in ) { | |
return true; | |
} | |
$user_logged_in = is_user_logged_in(); | |
if ( $user_logged_in && current_user_can( 'administrator' ) ) { | |
return false; | |
} | |
$restriction_setting = get_post_meta( $post->ID, '_wpumcr_restrict_access_post', true ); | |
if ( empty( $restriction_setting ) || 'yes' !== $restriction_setting ) { | |
return false; | |
} | |
$user_id = get_current_user_id(); | |
$allowed_users = wpumcr_get_complex_post_meta( $post->ID, 'wpumcr_assigned_users' ); | |
if ( ! empty( $allowed_users ) ) { | |
if ( in_array( $user_id, $allowed_users ) ) { | |
return false; // User is in one of the allowed users, don't restrict the post | |
} | |
} | |
$allowed_groups = wpumcr_get_complex_post_meta( $post->ID, 'wpum_groups' ); | |
foreach ( $allowed_groups as $allowed_group ) { | |
if ( wpumgrp_is_user_group_member( $allowed_group, $user_id ) ) { | |
return false; // User is in one of the allowed groups, don't restrict the post | |
} | |
} | |
return true; | |
}, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Save this file to your /wp-content/mu-plugins/ directory (you might need to create the mu-plugins directory).