Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

How to only allow logged in users to see comments to discussions?

Hi all!

What is the best way to achieve this? I want to make it so in order to see full comments on a discussion, you need to be a user of the forums. So any visitor can click on discussions and see the discussion itself, but in order to see the comments, they have to create a profile. An important caveat: I still want search engines to be able to index the site.

Thank you!

Best Answer

Answers

  • vrijvlindervrijvlinder Papillon-Sauvage MVP
    edited January 2015

    You can create a plugin which checks for valid session to load comments or not. I am not sure if this would have any effect on seo ...

    Or you could make a simpler plugin that only hides the comments from view via css.

         public function Base_Render_Before($Sender) {
                    $Session = Gdn::Session();
               // this tests to see is user is logged in or antixst
            if (!($Session->IsValid())) 
              $Sender->AddCssFile($this->GetResource('design/hidecomments.css', FALSE, FALSE));
         } else {
          return;
       }
    

    Then depending on the class names for the CommentsWrap in your theme, you write a css file with this in it .

        .CommentsWrap{
        display:none;
        }
    
  • Ah, got it! Okay, how would I display a message that says "In order to see the comments, please log in"?

    Anyone else know if this would block search engines from indexing the whole post?

  • BleistivtBleistivt Moderator
    edited January 2015

    It probably will, search engines are quite smart in figuring out what a user can see and what not.

    You could try to exclude bots from this by matching them by User-Agent String

    line 4 in vrijvlinders code:

    if (!$Session->IsValid() && !preg_match('/bot|crawl|slurp|spider/i', Gdn::Request()->GetValueFrom('server', 'HTTP_USER_AGENT')))
    

    taken from this SO answer: http://stackoverflow.com/a/15047834

    You can test if it works with a user agent switcher browser plugin.

  • hgtonighthgtonight ∞ · New Moderator

    @Bleistivt said:
    It probably will, search engines are quite smart in figuring out what a user can see and what not.

    You could try to exclude bots from this by matching them by User-Agent String

    line 4 in vrijvlinders code:

    if (!$Session->IsValid() && !preg_match('/bot|crawl|slurp|spider/i', Gdn::Request()->GetValueFrom('server', 'HTTP_USER_AGENT')))
    

    taken from this SO answer: http://stackoverflow.com/a/15047834

    You can test if it works with a user agent switcher browser plugin.

    Note that some people believe changing your site's behavior for bots will negatively affect your SEO mojo.

    If you believe in SEO, that is something to know.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • Yeah, I definitely don't want to try to trick any bots, so that's good to know. :)

    So what would the entire script look like to also show a line to prompt people to log in to see the comments?

    As you have figured out by now, I'm no coder, so as easy-to-implement as it can be, the better! ;)

    What would I add to this? Would the line go here?

    public function Base_Render_Before($Sender) { $Session = Gdn::Session(); // this tests to see is user is logged in or antixst if (!($Session->IsValid())) $Sender->AddCssFile($this->GetResource('design/hidecomments.css', FALSE, FALSE)); } else { return; <div>DOES IT GO HERE???</div> }

  • vrijvlindervrijvlinder Papillon-Sauvage MVP
    edited January 2015

    No you could add it as a css content or image text

    #Content .Message:before {
       content: "your message here";
    }
    

    Or you could use a message and pic after content. You can also echo the message in the code above but need to figure out where we want to echo it.

    echo '<span>text goes here</span>';

    Could also add the text as jquery which to me mgh be the easiest to append the text and you know where it goes then load the js file along with the css file.

        jQuery(document).ready(function($) { 
        $('.Message').append('<span>Please Log in to see the Comments</span>');
        });
    

    You can also add the log in link to that above

    jQuery(document).ready(function($) { 
            $('.Message').append('<a href="full link to the sign in page">Please Log in to see the Comments</a>');
            });
    

    http://css-tricks.com/css-content/

  • vrijvlindervrijvlinder Papillon-Sauvage MVP
    edited January 2015

    to be able to echo the text I think you would use this function

    public function Base_BeforeCommentDisplay_Handler($Sender) {

         <?php if (!defined('APPLICATION')) exit();
        // Define the plugin:
        $PluginInfo['NoComment'] = array(
           'Name'=>'NoComment',
           'Description' => "This plugin hides comments for guests and prompt them to log in to see them ",
           'Version' => '1.0',
           'MobileFriendly'=>TRUE,
           'Author' => "VrijVlinder",
           'License' => 'GNU GPL2'
           );
    
        class NoComment_Plugin extends Gdn_Plugin {
    
        public function Base_BeforeCommentDisplay_Handler($Sender) {
        if (!($Session->IsValid())) 
        echo '<span>Please log in to see the comments</span>';
        }
    
        public function Base_Render_Before($Sender) {
    
                        $Session = Gdn::Session();
                   // this tests to see is user is logged in or antixst
                if (!($Session->IsValid())) 
                  $Sender->AddCssFile($this->GetResource('design/nocomment.css', FALSE, FALSE));
                       } else {
              return;
           }
        public function Setup() {
           }
        }
    
  • BleistivtBleistivt Moderator
    edited January 2015

    You could also try hiding the comments with jQuery, that may even trick some search engines.

    Or blur out the comments: http://blurjs.com/

    Like quora does it. I hate it when sites do this, though.

  • vrijvlindervrijvlinder Papillon-Sauvage MVP
    edited January 2015

    Yes I hate that too... I think I need glasses even more.. :(

    What I would like to know is how important are comments in searches.. I mean when I search for something like why is my gas tank leaking gas, I get comments not necessarily the discussion title.

    Which leads to unsuccessful searches as far as quality content. search bot are stupid and only use words to match what you need. Comments may be gibberish. I would care more about the topic.

  • vrijvlindervrijvlinder Papillon-Sauvage MVP

    You could also.................

    It is amazing how many ways to skin katzen ... katzen geschinden ? oder gibt es einen anderen weg ?

  • We don't usually skin that many cats here, but we like taking different roads that all lead to rome.

  • vrijvlindervrijvlinder Papillon-Sauvage MVP
    edited January 2015

    We don't usually skin that many cats here

    You mean in Deutschland or here ? If here, yes , here only de-feather raptor birds...but I would avoid Rome.. it is full of sinners... ;)>:)

Sign In or Register to comment.