Skip to content

Instantly share code, notes, and snippets.

@hssktm
Created May 26, 2025 02:46
Show Gist options
  • Save hssktm/40fef212054bbafb8fd06ce185dd3cab to your computer and use it in GitHub Desktop.
Save hssktm/40fef212054bbafb8fd06ce185dd3cab to your computer and use it in GitHub Desktop.
Comments Woocommerce Error Messages
add_action('wp_ajax_check_duplicate_comment','check_duplicate_comment');
add_action('wp_ajax_nopriv_check_duplicate_comment','check_duplicate_comment');
function check_duplicate_comment(){
$post_id = intval($_POST['post_id'] ?? 0);
$content = trim(wp_unslash($_POST['content'] ?? ''));
$user_id = get_current_user_id();
global $wpdb;
$exists = $wpdb->get_var($wpdb->prepare(
"SELECT comment_ID FROM {$wpdb->comments} WHERE comment_post_ID=%d AND user_id=%d AND comment_content=%s AND comment_approved!='spam'",
$post_id, $user_id, $content
));
if($exists){
wp_send_json_error('Duplicate comment');
}
wp_send_json_success();
}
add_action('wp_footer','enqueue_comment_validation_script');
function enqueue_comment_validation_script(){
if(!is_singular() || !comments_open()) return;
$ajax_url = esc_url(admin_url('admin-ajax.php'));
?>
<script>
document.addEventListener('DOMContentLoaded',function(){
var form = document.getElementById('commentform');
if(!form) return;
var btn = form.querySelector('.form-submit input[type="submit"].submit');
btn.removeAttribute('id');
btn.removeAttribute('name');
btn.addEventListener('click',function(e){
e.preventDefault();
var prev = document.querySelector('.comment-error.wc-block-components-notice-banner.is-error');
if(prev) prev.remove();
var hasRating = form.querySelector('.stars').classList.contains('selected');
var content = form.querySelector('#comment').value.trim();
var hasComment = content.length > 0;
if(!hasRating || !hasComment){
var msg = !hasRating && !hasComment
? 'Select a rating and write a comment' //Change error message
: !hasRating
? 'Select a rating' //Change error message
: 'Write a comment'; //Change error message
var div = document.createElement('div');
div.className = 'comment-error wc-block-components-notice-banner is-error';
div.textContent = msg;
form.insertAdjacentElement('beforebegin', div);
return;
}
var data = new URLSearchParams();
data.append('action','check_duplicate_comment');
data.append('post_id', form.querySelector('[name="comment_post_ID"]').value);
data.append('content', content);
fetch('<?php echo $ajax_url; ?>', {
method: 'POST',
headers: {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'},
credentials: 'same-origin',
body: data.toString()
})
.then(function(res){ return res.json(); })
.then(function(json){
if(json.success){
HTMLFormElement.prototype.submit.call(form);
} else {
var div2 = document.createElement('div');
div2.className = 'comment-error wc-block-components-notice-banner is-error';
div2.textContent = json.data || 'Duplicate comment'; //Change error message
form.insertAdjacentElement('beforebegin', div2);
}
})
.catch(function(){
HTMLFormElement.prototype.submit.call(form);
});
});
});
</script>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment