I what create custom wordpress reply comment form and send data via ajax js , but when i send data to back are comments not show in admin panel. But status sending data is "OK" ( i cheked chrome console -> network ), how i can connect correctly front whith backend ? Whehere my mistake in this code ?
html
<div class="reply_comments respoond" style="display: none;">
<a class="cancel-comment-reply-link" style="display:none;">Cancel</a>
<form class="comment-form fcommentform">
<div class="comment-form-field-plus">
<label>Name:</label>
<input type="text" class="author" name="name">
<label>Email:</label>
<input type="text" class="email" name="email">
<textarea name="comment" class="comment" rows="5" cols="5" required></textarea>
</div>
<div class="form-footer">
<input class="like-post-button submit-comment-button rsubmit" type="button" value="<?php echo __('POST MY REVIEW', 'slotstory.com'); ?>" />
</div>
</form>
</div>
js
$('.rsubmit').on('click', function() {
$(this).closest('.fcommentform').submit();
});
$('.fcommentform').on('submit', function(e){
var errors = {
'name' : true,
'email' : true,
'comment' : true
};
e.preventDefault();
$(this).find('[name]').each(function( index ) {
var name = $( this ).attr('name');
var functionName = false;
switch(name){
case 'name':
functionName = _validateField;
break;
case 'email':
functionName = _validateEmail;
break;
case 'comment':
functionName = _validateComment;
break;
}
if (functionName) {
functionName.call(this);
$( this ).on('input', functionName.bind(this));
}
});
function _validateEmail(){
if(this != document) {
$element = $( this );
var reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (!reg.test($element.val())) {
$element.addClass('error');
errors[$element.attr('name')] = true;
} else {
delete errors[$element.attr('name')];
$element.removeClass('error');
}
}
}
function _validateField(){
if(this != document) {
$element = $( this );
if ($element.val() == '') {
$element.addClass('error');
errors[$element.attr('name')] = true;
} else {
delete errors[$element.attr('name')];
$element.removeClass('error');
}
}
}
function _validateComment(){
if(this != document) {
$element = $( this );
if ($element.val().length <= 10) {
$element.addClass('error');
errors[$element.attr('name')] = true;
} else {
delete errors[$element.attr('name')];
$element.removeClass('error');
}
}
}
if (Object.keys(errors).length == 0) {
console.log('success');
}
$.ajax({
type : 'POST',
url: ajaxactionurl,
data: $(this).serialize() + '&action=ajaxcomments',
error: function (request, status, error) {
if( status == 500 ){
alert( 'Error while adding comment' );
} else if( status == 'timeout' ){
alert('Error: Server doesn\'t respond.');
} else {
var wpErrorHtml = request.responseText.split("<p>"),
wpErrorStr = wpErrorHtml[1].split("</p>");
alert( wpErrorStr[0] );
}
},
});
return false;
});
php
add_action( 'wp_ajax_ajaxcomments', 'custom_submit_ajax_comment' );
add_action( 'wp_ajax_nopriv_ajaxcomments', 'custom_submit_ajax_comment' );
function custom_submit_ajax_comment() {
global $wpdb;
$comment = wp_handle_comment_submission( wp_unslash( $_POST ) );
if ( is_wp_error( $comment ) ) {
$error_data = intval( $comment->get_error_data() );
if ( ! empty( $error_data ) ) {
wp_die( '<p>' . $comment->get_error_message() . '</p>', __( 'Comment Submission Failure' ), array( 'response' => $error_data, 'back_link' => true ) );
} else {
wp_die( 'Unknown error' );
}
}
$comment_data = array(
'comment_post_ID' => $comment_post_ID,
'comment_content' => $comment,
'comment_parent' => $comment_parent,
'comment_date' => $time,
'comment_approved' => 0,
'user_id' => $current_user->ID,
);
exit();
}