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
Hi @pyue6299,
Please use this code:
add_action('wpdiscuz_add_comment', function () {
if (!comments_open(intval($_POST['postId']))) {
die(__('Your message'));
}
});
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
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!
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.
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.