Minesh had just helped me get comments to programmatically disappear from single pages if a user did not have proper credentials (based on a combination of their user role and the term associated with the post in the "access control" taxonomy). We thought we had it working but that no longer appears to be the case.
Below please find an example page that is not working (comments will appear even if unauthenticated):
lien caché
lien caché
Below is a link to the original ticket:
https://toolset.com/forums/topic/disabling-comments-programmatically-on-a-single-page/page/2/
Below is the code that we had implemented:
add_action( 'template_redirect', 'pp_disable_comments' );
function pp_disable_comments($post_object) {
// only run if single page
if (is_single()) {
$pp_disable_comments = false;
$user = wp_get_current_user();
$roles = (array) $user->roles;
// skip if user is contributor/author/editor/administrator OR if the post is public
if ((!in_array('contributor', $roles) && !in_array('contributor', $roles) && !in_array('author', $roles) && !in_array('editor', $roles) && !in_array('administrator', $roles)) || has_term('public', 'access-control')) {
// run if post is tier-0 only
if (has_term('tier-0', 'access-control')) {
// check if user not in allowed roles
if (!in_array('tier-0', $roles) && !in_array('tier-1', $roles) && !in_array('tier-2', $roles)) {
$pp_disable_comments = true;
}
}
// run if post is tier-1 only
if (has_term('tier-1', 'access-control')) {
// check if user not in allowed roles
if (!in_array('tier-1', $roles) && !in_array('tier-2')) {
$pp_disable_comments = true;
}
}
// run if post is tier-2 only
if (has_term('tier-2', 'access-control')) {
// check if user not in allowed roles
if (!in_array('tier-2', $roles)) {
$pp_disable_comments = true;
}
}
}
// disable comments if required
if ($pp_disable_comments == true) {
global $post;
$post->comment_status="closed";
}
}
}
This was a duplicate submission. Your website gave an error message when I submitted it the first time and when I went to my tickets it didn't appear on the list because it was being cached.
- Aaron