如何通过AJAX添加WP注释

时间:2019-06-06 08:19:01

标签: javascript php ajax wordpress comments

我创建自定义wordpress回复评论表单并通过ajax js发送数据的方法,但是当我向后发送数据时,评论不会显示在管理面板中。但是状态发送数据为“ OK”(我检查了chrome控制台-> network),如何正确连接前端和后端?我在此代码中的错误在哪里?

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();
}

1 个答案:

答案 0 :(得分:0)

状态为200,因为您没有在php中发送任何内容。
我建议检查响应的正文,而不仅仅是状态码。

在那里开始调试。祝你好运。