Joomla K2 Comment Notification Hack

The website content (articles, categories, comments etc) are all part of the "K2" system installed on the site. This is an "advanced" article management system that adds a heap of extra features over the standard way Joomla manages articles on the site. All the lists and layout for the articles you read (including this one) are managed by this Joomla component. 

As with most things when designing a website, there is not a "1 solution fits all" and there have been some problems to overcome which is why it takes sooooo long to get everything going. 

The Issue

One issue I discovered about this K2 system is the comment feature at the end of articles which allow visitors to leave their comments does not notify the web administrator (me) of new comments. This seems strange to me since most other comment systems I have tested have this as a basic feature.

Jcomments for instance has not only this feature but also the ability to delete comments or edit them directly from the email sent which means if someone posts a spam or offensive comment it can be removed in minutes just using my phone.

I wanted the integrated feature of the K2 comment system and simply wanted to know if a user has posted comments to an article and know the contents of what they posted. It appeared the only "plugin" solution would cost $20 and considering the K2 framework component was free it seemed a bit of a ripoff for a single simple feature.

The Hack Fix

I spent some time researching solutions and came over a posted hack that allowed a notification to be sent if you had comments NOT set to auto-publish. I wanted user comments to publish automatically so after some messing around I managed to create a simple solution that will send the following email:

 


 

 From Freelance Soundlabs

To This email address is being protected from spambots. You need JavaScript enabled to view it.
Date Today 10:54
Priority Normal


New User Comment Posted:
Name: test
Email: This email address is being protected from spambots. You need JavaScript enabled to view it.
Website: www.testwebsite.com
comment: This is a test comment with some words that say things to make up the volume of the message body so there is something in the damn message!!!

 


 

This will be sent whenever someone publishes a comment and has all the info to decide if it is a genuine comment or spam to be actioned. The email it is sent too is the email set in the K2 parameters for the Comment Reporting email where users can report comments to the admin. 

This hack requires editing a file and will require this edit to be redone if the component is updated so it is not a perfect solution but a workable one. To apply it to your site follow these steps:

1. Open the file: components/com_k2/models/item.php
2. Search in the file for the line that contains: K2_COMMENT_ADDED_REFRESHING_PAGE
3. Directly above that line paste the following chunk of code:

$mainframe = &JFactory::getApplication();
$mail = &JFactory::getMailer();
$senderEmail = $mainframe->getCfg('mailfrom');
$senderName = $mainframe->getCfg('fromname');
$mail->setSender(array($senderEmail, $senderName));
$mail->setSubject(JText::_('K2_NEW_COMMENT'));
$mail->IsHTML(true);
$body = "
".JText::_('New User Comment Posted').":

".JText::_('K2_NAME').": ".$userName."

".JText::_('K2_EMAIL').": ".$commentEmail."

".JText::_('K2_WEBSITE').": ".$commentURL."

".JText::_('K2_COMMENT').": ".nl2br($row->commentText)."
";
$mail->setBody($body);
$mail->ClearAddresses();
$mail->AddAddress($params->get('commentsReportRecipient', $mainframe->getCfg('mailfrom')));
$mail->Send();


Make sure you have an email set in the K2 parameters/comments section for comment reports.

Test by posting a new comment and you should receive an email. 

If you want to set an email for comments to be "approved" then do exactly the same as above but search for the code: K2_COMMENT_ADDED_AND_WAITING_FOR_APPROVAL

The only extra information I would love to include in the message would be the article the comment was posted too. I am not an expert in this stuff so I am not sure but maybe a Joomla expert will help me out one day. For now, this works just fine.

 

 

 

Last modified on Thursday, 12 December 2013 15:34

Go to top