Keyword

Disable comments after x days

  • Steven Trooster
  • Steven Trooster's Avatar Topic Author
  • Offline
  • Senior Member
More
2 months 3 weeks ago - 2 months 3 weeks ago #170745 by Steven Trooster
Steven Trooster created the topic: Disable comments after x days
I've seen that spammers like to post in old articles, so they won't get noticed. Is there a way to disable comments to an article automatically after a number of days? Now I have to set / unset the comments per article, which is undoable since we post 10 articles a week.
And I don't want the comments already posted to disappear, just the form removed from the article.
Last edit: 2 months 3 weeks ago by Steven Trooster.

Please Log in or Create an account to join the conversation.

  • Fotis
  • Fotis's Avatar
  • Offline
  • Administrator
  • JoomlaWorks Support Team
More
2 months 3 weeks ago #170749 by Fotis
Fotis replied the topic: Disable comments after x days
You have 2 options here. Either enable moderation (no comments are auto-published) or add some PHP to check if the item was created more than 10 days ago and then hide the form entirely.

At the very top of your item.php override and after "defined('_JEXEC') or die;", add this inside the <?php ... ?> block:
$itemDate = strtotime($this->item->created);
$now = strtotime('now');
$diff = number_format(($now - $itemDate)/(3600*24));

"$diff" returns the difference in days between now and the time the item was created. So if you want to hide the comments form 10 days after an item was created, update the 2 instances of "<?php echo $this->loadTemplate('comments_form'); ?>" with:
<?php if ($diff <= 10): ?>
<?php echo $this->loadTemplate('comments_form'); ?>
<?php endif; ?>


If you use & love K2, please take a moment to add your review and rate it
at the Joomla Extensions Directory: extensions.joomla.org/extension/k2/


IMPORTANT: Please search the forum before posting a question!

JoomlaWorks Support Team Member

Please Log in or Create an account to join the conversation.

  • Steven Trooster
  • Steven Trooster's Avatar Topic Author
  • Offline
  • Senior Member
More
2 months 2 weeks ago #170843 by Steven Trooster
Steven Trooster replied the topic: Disable comments after x days
Moderation will have an extra burden on the admins.
Adding extra code to the template was something I was thinking too. Thank you for the code. That saves me some time.

Please Log in or Create an account to join the conversation.

  • Fotis
  • Fotis's Avatar
  • Offline
  • Administrator
  • JoomlaWorks Support Team
More
2 months 2 weeks ago #170847 by Fotis
Fotis replied the topic: Disable comments after x days
You're welcome :)


If you use & love K2, please take a moment to add your review and rate it
at the Joomla Extensions Directory: extensions.joomla.org/extension/k2/


IMPORTANT: Please search the forum before posting a question!

JoomlaWorks Support Team Member

Please Log in or Create an account to join the conversation.

Moderators: william white

Powered by Kunena Forum