[Résolu] Disabling Comments Programmatically on a Single Page
This support ticket is created Il y a 3 années et 1 mois. There's a good chance that you are reading advice that it now obsolete.
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.
Well - honestly I would like to help you but until and unless I check your setup it would be really time consuming as it will be like I'm sharing the solution without knowing anything.
Do you have any other setup where I can apply the code - is it possible for you to setup another test site on another test install where I can play it and get back to you with the solution?
Okay, I have an idea. Go to sandbox.peakprosperity.com instead. It's an older clone of the development site but I've copied over the code snippet and you should be able to trouble shoot there. I made sure the password is the same as the one previously sent (ending in 4YZm). You shouldn't need anything in your hosts file for this. Let me know if that works. Thanks.
We will use the WordPress hook "template_redirect" and I've adjusted the code.
I've adjusted the code as given under within the code snippet:
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;
// NEED TO CAHNGE ADMINISTRATOR BACK AFTER DONE TESTING
// 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('xxadministrator', $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";
}
}
}
When I visit the following page I can see the comment section is not available: lien caché
That means comment section is gone that is expected result.
Can you please confirm it works at your end as well.
I can confirm it appears to be working now. It is not showing comments when unauthenticated and it is showing comments when authenticated. Thanks for the help!