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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀