weixin_33712881 2019-06-06 08:19 采纳率: 0%
浏览 33

如何通过AJAX添加WP注释

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();
}
  • 写回答

1条回答 默认 最新

  • weixin_33743248 2019-06-06 08:33
    关注

    The status is 200 ok because you don't send anything in the php.
    I suggest checking the body of the response, not just the status code.

    Start debugging there. Good luck.

    评论

报告相同问题?

悬赏问题

  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。