• Hi
    I have been searching high and low to find something that will allow me to stop unregistered browsers from reading comments on posts.

    There are some sophisticated plugins that will probably work but they appear overkill and expensive !

    So I found the following script which apparently only allow the post author and the commentator to see the comment – it seems perfect for me but I don’t know where to put it !

    <em>function restrict_comments( $comments , $post_id ){ 
    
    global $post;
    
    $user = wp_get_current_user();
    
    if($post->post_author == $user->ID){
    
    		return $comments;
    
    }
    
    foreach($comments as $comment){
    
    	if(  $comment->user_id == $user->ID || $post->post_author == $comment->user_id  ){
    
    		if($post->post_author == $comment->user_id){
    
    			if($comment->comment_parent > 0){
    
    				$parent_comm = get_comment( $comment->comment_parent );
    
    				if( $parent_comm->user_id == $user->ID ){
    
    					$new_comments_array[] = $comment;		
    
    				}
    
    			}else{
    
    					$new_comments_array[] = $comment;	
    
    			}
    
    		}else{
    
    			$new_comments_array[] = $comment;			
    
    		}
    
    	}
    
    }
    
     return $new_comments_array; }
    
    add_filter( 'comments_array' , 'restrict_comments' , 10, 2 );
    </em>

    Maybe in Functions.php or comments.php ?

    Any help will be gratefully received 🙂

    the site is http://www.thewatchwithnoname.com
    It is in experimental mode at the moment !

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Yes, functions.php would work fine, though it could get overwritten when the theme is updated. To protect it, consider making a child theme. Then your code would go on functions.php of your child theme and remain safe.

    The other option is creating a site-specific plugin. It’s a handy place to contain all of your little hacks and tweaks for the site. A basic plugin is quite easy to create. If you are doing template hacks then a child theme is a better choice.

Viewing 1 replies (of 1 total)

The topic ‘Where do I put this nice little script please ?’ is closed to new replies.