Skip Navigation

[Résolu] Disabling Comments Programmatically – Follow-up

This support ticket is created Il y a 3 années. 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.

Ce sujet contient 1 réponse, a 1 voix.

Dernière mise à jour par aaronM-9 Il y a 3 années.

Auteur
Publications
#2210793

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";
             
        }
         
    }
     
}
#2210809

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