Notifications
Clear all

Action hook in class.WpdiscuzCore.php

8 Posts
2 Users
0 Likes
1,118 Views
(@pyue6299)
Member Customer
Joined: 6 years ago
Posts: 53
Topic starter  

Hi author

I would like to change message in the following red message in class.WpdiscuzCore.php line 379 using action hook:

do_action('wpdiscuz_add_comment');
if (!comments_open($postId)) {
die(__('We are sorry, you are not allowed to comment more than one time!', 'wpdiscuz'));
}

And i would like to know how can i use action hook in function.php to do this? or any other way you suggest?

Thanks

Patrick

This topic was modified 5 years ago by pyue6299

   
Quote
Astghik
(@astgh)
Illustrious Member Admin
Joined: 6 years ago
Posts: 5908
 

Hi @pyue6299,

Please use this code: 

add_action('wpdiscuz_add_comment', function () {
if (!comments_open(intval($_POST['postId']))) {
die(__('Your message'));
}
});

   
ReplyQuote
(@pyue6299)
Member Customer
Joined: 6 years ago
Posts: 53
Topic starter  

Yes it is working perfectly. In fact, i added the below code to limit my user 1 comment per post:

add_filter('comments_open', 'restrict_users', 10, 2);

function restrict_users($open, $post_id) {
if (intval($post_id) && get_post($post_id)) {
$args = array('post_id' => $post_id, 'count' => true);
$user = wp_get_current_user();
if ($user && intval($user->ID)) { // for registered users
$skip = false;
$ignoreTheseRoles = array('administrator', 'editor'); // which user roles should be ignored
if ($user->roles && is_array($user->roles)) {
foreach ($user->roles as $role) {
if (in_array($role, $ignoreTheseRoles)) {
$skip = true;
break;
}
}
}
if (!$skip) {
$args['user_id'] = $user->ID;
$open = get_comments($args) ? false : true;
}
} else { // for guests
$commenter = wp_get_current_commenter();
if ($commenter && is_array($commenter) && isset($commenter['comment_author_email'])) {
$args['author_email'] = $commenter['comment_author_email'];
$open = get_comments($args) ? false : true;
}
}
}
return $open;

After refresh, the comment form disappeared, which is nice but can i also add a paragraph that explains to my user that they have already posted one comment so they cannot post it twice?

Many thanks


   
ReplyQuote
Astghik
(@astgh)
Illustrious Member Admin
Joined: 6 years ago
Posts: 5908
 

@pyue6299

You can use the following hook to display the message: 

wpdiscuz_comment_form_closed

   
ReplyQuote
(@pyue6299)
Member Customer
Joined: 6 years ago
Posts: 53
Topic starter  

Can you teach me on how to write the complete code if i want to display the below message:

"You have already provided a quotation, thanks"

 

Thanks for your help!


   
ReplyQuote
Astghik
(@astgh)
Illustrious Member Admin
Joined: 6 years ago
Posts: 5908
 

@pyue6299,

The solution is provided below:

add_action('wpdiscuz_comment_form_closed', function () {
echo __('You have already provided a quotation, thanks');
});

You should add it in active theme's functions.php file. 


   
ReplyQuote
(@pyue6299)
Member Customer
Joined: 6 years ago
Posts: 53
Topic starter  
Posted by: @pyue6299

Yes it is working perfectly. In fact, i added the below code to limit my user 1 comment per post:

add_filter('comments_open', 'restrict_users', 10, 2);

function restrict_users($open, $post_id) {
if (intval($post_id) && get_post($post_id)) {
$args = array('post_id' => $post_id, 'count' => true);
$user = wp_get_current_user();
if ($user && intval($user->ID)) { // for registered users
$skip = false;
$ignoreTheseRoles = array('administrator', 'editor'); // which user roles should be ignored
if ($user->roles && is_array($user->roles)) {
foreach ($user->roles as $role) {
if (in_array($role, $ignoreTheseRoles)) {
$skip = true;
break;
}
}
}
if (!$skip) {
$args['user_id'] = $user->ID;
$open = get_comments($args) ? false : true;
}
} else { // for guests
$commenter = wp_get_current_commenter();
if ($commenter && is_array($commenter) && isset($commenter['comment_author_email'])) {
$args['author_email'] = $commenter['comment_author_email'];
$open = get_comments($args) ? false : true;
}
}
}
return $open;

After refresh, the comment form disappeared, which is nice but can i also add a paragraph that explains to my user that they have already posted one comment so they cannot post it twice?

Many thanks

I discovered that the above code conflicts with "diable comment for roles" in the  Form section. If i insert the above code in function.php, the disable for roles commenting function will be lost. Please advice.


   
ReplyQuote
Astghik
(@astgh)
Illustrious Member Admin
Joined: 6 years ago
Posts: 5908
 

@pyue6299

Please provide some screenshots of the conflict you've mentioned. 


   
ReplyQuote
Share:
Scroll to top