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条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 macOS使用IDEA 2022.2.4打包JDK6项目报错
      • ¥20 OpenCV-Python简单轮廓寻找
      • ¥20 使用matlab进行含参数的最优化求解及数值模拟
      • ¥20 MATLAB找出一维变量中的局部极大值和局部极小值,并进行运算
      • ¥15 有Chang求三维杆单元几何非线性分析matlab代码
      • ¥50 使用unity easyar录屏功能。录屏视频整体色调很暗
      • ¥50 pyinstaller 打包问题
      • ¥15 无法使用cnssc调取网页数据
      • ¥15 Pycharm安装en_core_web_sm失败出现Could not find a version that satisfies the requirement spacy报错
      • ¥15 Python社区划分