Recent Topics

Require user login to comment

Started by on Sep 24, 2004 – Contents updated: Sep 24, 2004

Sep 24, 2004 23:43    

I would like to make it a requirement for a viewer to be a user to be able to comment on a post. However, the posts should still be viewable to the public. Basically, this system should allow one level of users to post and then a 2nd level of users to be able to comment and then anonymous to view.

It seems so simple, but I just can't seem to find this in b2evolution!

Sep 25, 2004 00:11

It's not a feature as far as I know. Anyone who can see a post can comment IF comments are not closed or disabled. Closing or disabling will also affect registered bloggers, so that's not what you seek. It's an easy hack if you don't mind going down that path, but the hack will affect all posts under all circumstances - you won't be able to say "posts in this blog can be publicly seen and commented, but posts in that blog can be publicly seen but not commented. Also, it will not over-ride the closed or disabled condition.

Every skin has a skinname, and every skins/skinname/ folder has a bunch of files in it. The skins/ folder tends to have the same file names. Open skins/_feedback.php and look down around line 140 for this


if( $disp_comment_form )
  {  // We want to display the comments form:
    if( $Item->can_comment() )
    { // User can leave a comment
and change it to this


if( $disp_comment_form && is_logged_in() )
  {  // We want to display the comments form to a registered member:
    if( $Item->can_comment() )
    { // User can leave a comment

Save the file and upload it. Live long and prosper.

Be aware I have not actually done this. It just seemed like a pretty straightforward hack.

Sep 25, 2004 00:21

Sweet - it worked! :D
Thanks, I'm just getting familar with b2evolution (switching from wordpress because of lack of multiple blogs).

I just have to make it a little prettier now (so it doesn't display Post Comment at all).

Mar 22, 2005 12:19

Brilliant! I'll have to try it as soon as I get home from work. Just what I was after! :D I was pingback-spammed before, but switched it off, only to find myself being comment-spammed instead. *groan*

EDIT: Oooh I love cPanel. I could do it from work using the cPanel File Manager. File changed and it works! Hooray! :D Now I might even decide whether or not to allow users to register...

Mar 22, 2005 19:52

Hmm I may have to try that, although I like the comments to be from whoever, not make em signup. I hate signing up for things :-)

Mar 23, 2005 19:16


I added an extra code block just above the code that Ed mentions. The point of the code is just to display a message that you have to be logged in before leaving a comment:


if( $disp_comment_form && !is_logged_in() )
    echo "You must be logged in to add a comment.<br />";

Just a little cleaner.



Jun 14, 2005 22:32

I believe $htsrv_url/comment_post.php would also need to be modified with a similar !is_logged_in() check since a robot or human could post directly to $htsrv_url/comment_post.php without using the form on skins/_feedback.php.

This could be done using the following code:


if( is_logged_in() )
{ // User is loggued in, we'll use his ID
    // (code snipped for brevity)
    errors_add( T_('You cannot leave comments on this post!') );
    // (code snipped for brevity)

Jun 18, 2005 09:20

I've followed the tips in this thread and have it working on our development blog. I'm happy to have users logon to comment cos we've got lots of schools on our systems and we've got to keep a tight rein on this.

My prob is I have been trying to add a link to allow visitors direct access to logon or register. My code is below. The line I added to try to set up the link is about third down starting with <a href etc etc - don't laugh :oops: I know it doesn't work but nor does all kinds of variants that I have tried


echo "You must be logged in to add a comment.<br />";
<a href="<?php echo $admin_url ?>"><?php echo T_('Click here to logon or register') ?></a>
   if( $disp_comment_form && is_logged_in() )
     {   // We want to display the comments form to a registered member:
        if( $Item->can_comment() )
        { // User can leave a comment

Jun 22, 2005 19:03

Replace the You Must be logged in to post a comment with:
user_login_link( '', '','Login/Register to Comment' );
And you will have a better result

Also to stop wasting the users time with 'leave a comment' when they cant post replace the $zero stuff in _class_item.php with this:


if( $zero == '#' ){
          if(is_logged_in() ){
            $zero = T_('Leave a comment');
            $zero = T_('');

Or alternatively giving a login/register link:


echo $before;
    if( $number == 0 && $zero == 'Leave a comment' && !is_logged_in() ){
      user_login_link( '', '','Login/Register to comment' );
    } else {
      echo '<a href="', $url;
      echo '#', $type, '" ';  // Position on feedback
      echo 'title="', $title, '"';
      if( $use_popup ) echo ' onclick="b2open(this.href); return false"';
      echo '>';
      if( $number == 0 )
        echo $zero;
      elseif( $number == 1 )
        echo $one;
      elseif( $number > 1 )
        echo str_replace( '%d', $number, $more );
      echo '</a>';
    echo $after;

Jul 03, 2005 22:15

Is it possible to only allow registered users who are a certain level to leave comments?

May 21, 2006 23:04

just runing in the circle for hours now.
I installed the final capcha hack and it works fine.
Now i whant to aktivate back anonymous comment get posible.
Hacked the forum befor with EdB solution.
To prevent all the spams on the blog.
But when i go back the modification comments dont work
It comex back witha SQL erorr User ID is missing .
That hack i used and remove the logged in part.


if( $disp_comment_form && is_logged_in() ) { // We want to display the comments form:
    if( $Item->can_comment() ) { // User can leave a comment ?>

Hope its not too confusing
any help will give me some sleep back
thnaks in advance

May 22, 2006 11:12

Found the line witch make the SQL EROR


//$current_User_Data = get_userdata( $current_User->ID );

Quotet it out but is ther a other way to get it work like it shoud .
Sorry my php is knowlage is more like zero
and till now everything worked fine
hope this forum is still alive

Jun 01, 2006 07:44

ok i am completely new to this and very confused. i followed ed's advice and wrote the code into feedback but it didn't seem to do anything.

I don't want anyone to be able to post without being logged in. and if they're not logged i want it to direct them to a page where they can.

Can anyone help

Jun 01, 2006 09:06

Hi CArringt. You personally should not notice a difference unless you log out. Or check your blog in a different browser. I suggest using IE to test stuff since all smart people use Firefox as their primary browser ;)

There have obviously been a couple of suggested improvements to this hack. I'm sure that for the person who offered them they're good ideas, but I must say I've no idea what version I made this hack work on. I am currently using an old CVS version, and I no longer use this hack at all. I use my Simple Turing Test, which can be found by either looking through the "fighting spam" forum or using the search feature.

One other thing to keep in mind. The person who mentioned that this won't stop spam-bots from directly hitting htsrv/comment_post.php is completely correct. The only thing that will happen is that an unregistered human will see that they have to register in order to comment. That will cut down on drive-by comments, but will not thwart the evil intentions of comment spammers.

Jun 01, 2006 16:22

Thanks EdB. i'll keep searching around. i appreciate the wordds of wisdom

Jun 09, 2006 00:30

EdB, how about you post this as a nice clean hack? Make it nice and straight forward?


Jun 11, 2006 02:14

Sure. I think it's a nice hack and a lot of people have added good bits to it. Look for it in the "plugins and hacks" forum, or maybe the "got spam?" sub-forum. When I post a composite over there I'm going to lock this thread because we should move discussion about hacks over to the appropriate forum. Oh and I'll be working with 0.9.2 to make this composi-hack happen. IF it also works in 1.6 I'll let everyone know.

Form is loading...

Photo gallery software – This forum is powered by b2evolution CMS, a complete engine for your website.