• Resolved Anonymous User 18187419

    (@anonymized-18187419)


    Hi Michael,

    I am trying to put together a bit of code to prevent moderated users from commenting in the regular WP blog pages.

    Desired function:
    – regular users not affected.
    – moderated users can view blog pages, can view comments left by others, but comment box is hidden from them and they are shown “Comments are closed” message.

    My code is below, seems to work as desired on my test site. Would you please be kind enough to take a look in case I am making some errors? I’m not a coder so I need a thumbs up (or down) before I add it to my live site.

    I appreciate your help, thank you.

    function remove_comment_box_if_moderated() {
    
         $current_user = bp_loggedin_user_id();
         
         if ( bp_registration_get_moderation_status( $current_user ) ) {
                  add_filter( 'comments_open' , '__return_false' );
                  }
         else
                  {
                  add_filter( 'comments_open' , '__return_true' );
                  }
    }
    
         add_filter( 'init' , 'remove_comment_box_if_moderated' );
    
Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter Anonymous User 18187419

    (@anonymized-18187419)

    Hmmm … after more testing, this code is no good. It unfortunately forces comments to be allowed on pages, irrespective if you want that function or not.

    I normally only allow comments on posts, not pages.

    Michael, any assistance please?

    Thread Starter Anonymous User 18187419

    (@anonymized-18187419)

    Okay, another attempt…
    – will not show comment box on pages
    – will not show comment box for moderated users
    ….

    Any good?

    
    function remove_comment_box_if_moderated() {
    
         $current_user = bp_loggedin_user_id();
         
         if (is_page() | bp_registration_get_moderation_status( $current_user ) ) {
         add_filter( 'comments_open' , '__return_false' );
         }                                            
    }
    
         add_filter( 'init' , 'remove_comment_box_if_moderated' );
    
    Michael Beckwith

    (@tw2113)

    The BenchPresser

    Technically, if it works and does what you want, it’s good. However, I would probably implement something like this if I was tasked with this request

    Return early if not on a single post. If on a single post, check moderated status and remove comments being open if still moderated.

    function remove_comment_box_if_moderated() {
    	if ( ! is_single() ) {
    		return;
    	}
    
    	$current_user = bp_loggedin_user_id();
    
    	if ( true === bp_registration_get_moderation_status( $current_user ) ) {
    		add_filter( 'comments_open' , '__return_false' );
    	}
    }
    add_filter( 'init' , 'remove_comment_box_if_moderated' );
    
    Thread Starter Anonymous User 18187419

    (@anonymized-18187419)

    Thanks Michael!

    Michael Beckwith

    (@tw2113)

    The BenchPresser

    Welcome.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Prevent moderated users from commenting on WP blog’ is closed to new replies.