尝试在WordPress中实现AJAX注释,遇到WP错误

时间:2022-01-13 作者:kstach001

我试图在WordPress中为我的评论实现Ajax,使用this tutorial. 但我在将教程中的代码集成到自己的预构建主题时遇到了问题。问题是,我要么得到一个WP错误“检测到重复注释;看来你已经说过了!”或标准500错误。以下是我得到的:

下面是我对ajax的评论。js文件如下所示:

 * Let\'s begin with validation functions
 */
 jQuery.extend(jQuery.fn, {
    /*
     * check if field value lenth more than 3 symbols ( for name and comment ) 
     */
    validate: function () {
        if (jQuery(this).val().length < 3) {jQuery(this).addClass(\'error\');return false} else {jQuery(this).removeClass(\'error\');return true}
    },
    /*
     * check if email is correct
     * add to your CSS the styles of .error field, for example border-color:red;
     */
    validateEmail: function () {
        var emailReg = /^([\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4})?$/,
            emailToValidate = jQuery(this).val();
        if (!emailReg.test( emailToValidate ) || emailToValidate == "") {
            jQuery(this).addClass(\'error\');return false
        } else {
            jQuery(this).removeClass(\'error\');return true
        }
    },
});
 
jQuery(function($){

    /*
     * On comment form submit
     */
    $( \'#commentform\' ).submit(function(){
    
        // define some vars
        var button = $(\'#submit\'), // submit button
            respond = $(\'#respond\'), // comment form container
            commentlist = $(\'.comment-list\'), // comment list container
            cancelreplylink = $(\'#cancel-comment-reply-link\');
            
        // if user is logged in, do not validate author and email fields
/*
        if( $( \'#author\' ).length )
            $( \'#author\' ).validate();
        
        if( $( \'#email\' ).length )
            $( \'#email\' ).validateEmail();
*/
            
        // validate comment in any case
        $( \'#comment\' ).validate();
        
        // if comment form isn\'t in process, submit it
        if ( !button.hasClass( \'loadingform\' ) && !$( \'#author\' ).hasClass( \'error\' ) && !$( \'#comment\' ).hasClass( \'error\' ) ){
            
            // ajax request
            $.ajax({
                type : \'POST\',
                url : html5_comment_params.ajaxurl, // admin-ajax.php URL
                data: $(this).serialize() + \'&action=ajaxcomments\', // send form data + action parameter
                beforeSend: function(xhr){
                    // what to do just after the form has been submitted
                    button.addClass(\'loadingform\').val(\'Loading...\');
                },
                error: function (request, status, error) {
                    if( status == 500 ){
                        alert( \'Error while adding comment\' );
                    } else if( status == \'timeout\' ){
                        alert(\'Error: Server doesn\\\'t respond.\');
                    } else {
                        // process WordPress errors
                        var wpErrorHtml = request.responseText.split("<p>"),
                            wpErrorStr = wpErrorHtml[1].split("</p>");
                            
                        alert( wpErrorStr[0] );
                    }
                },
                success: function ( addedCommentHTML ) {
                
                    // if this post already has comments
                    if( commentlist.length > 0 ){
                    
                        // if in reply to another comment
                        if( respond.parent().hasClass( \'comment\' ) ){
                        
                            // if the other replies exist
                            if( respond.parent().children( \'.children\' ).length ){  
                                respond.parent().children( \'.children\' ).append( addedCommentHTML );
                            } else {
                                // if no replies, add <ol class="children">
                                addedCommentHTML = \'<ol class="children">\' + addedCommentHTML + \'</ol>\';
                                respond.parent().append( addedCommentHTML );
                            }
                            // close respond form
                            cancelreplylink.trigger("click");
                        } else {
                            // simple comment
                            commentlist.append( addedCommentHTML );
                        }
                    }else{
                        // if no comments yet
                        addedCommentHTML = \'<ol class="comment-list">\' + addedCommentHTML + \'</ol>\';
                        respond.before( $(addedCommentHTML) );
                    }
                    // clear textarea field
                    $(\'#comment\').val(\'\');
                },
                complete: function(){
                    // what to do after a comment has been added
                    button.removeClass( \'loadingform\' ).val( \'Post Comment\' );
                }
            });
        }
        return false;
    });
});
我已经在我的函数中注册并排队了它。php文件,如下所示:


function misha_ajax_comments_scripts() {
 
    // I think jQuery is already included in your theme, check it yourself
    wp_enqueue_script(\'jquery\');
 
    // just register for now, we will enqueue it below
    wp_register_script( \'ajax_comment\', get_stylesheet_directory_uri() . \'/ajax-comment.js\', array(\'jquery\') );
 
    // let\'s pass ajaxurl here, you can do it directly in JavaScript but sometimes it can cause problems, so better is PHP
    wp_localize_script( \'ajax_comment\', \'html5_comment_params\', array(
        \'ajaxurl\' => site_url() . \'/wp-admin/admin-ajax.php\'
    ) );
 
    wp_enqueue_script( \'ajax_comment\' );
}
并且还利用了我的主题用于注释的自定义函数(也在functions.php中):



add_action( \'wp_ajax_ajaxcomments\', \'html5blankcomments\' ); // wp_ajax_{action} for registered user
add_action( \'wp_ajax_nopriv_ajaxcomments\', \'html5blankcomments\' ); // wp_ajax_nopriv_{action} for not registered users

// Custom Comments Callback
function html5blankcomments($comment, $args, $depth)
{
    
    $comment = wp_handle_comment_submission( wp_unslash( $_POST ) );
    
    extract($args, EXTR_SKIP);
    
    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\' );
        }
    }

    if ( \'div\' == $args[\'style\'] ) {
        $tag = \'div\';
        $add_below = \'comment\';
    } else {
        $tag = \'li\';
        $add_below = \'div-comment\';
    }
    
    /*
     * Set Cookies
     */
    $user = wp_get_current_user();
    do_action(\'set_comment_cookies\', $comment, $user);
    
    $GLOBALS[\'comment\'] = $comment;
?>
    <!-- heads up: starting < for the html tag (li or div) in the next line: -->
    <<?php echo $tag ?> <?php comment_class(empty( $args[\'has_children\'] ) ? \'\' : \'parent\') ?> id="comment-<?php comment_ID() ?>">
    <?php if ( \'div\' != $args[\'style\'] ) : ?>
    <div id="div-comment-<?php comment_ID() ?>" class="comment-body">
    <?php endif; ?>
    
    <div class="comment-author vcard">
    
    <?php printf(__(\'<cite class="fn">%s</cite><span class="says"> </span\'), get_comment_author_link()) ?><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>"></a>
    
        <?php
                        printf( _x( \'%s ago\', \'%s = human-readable time difference\', \'your-text-domain\' ), human_time_diff( get_comment_time( \'U\' ), current_time( \'timestamp\' ) ) );
                        ?>
    </div>
    

    

    <?php comment_text() ?>
    
    <?php if( get_field(\'commenter_rating_number\', $comment) ): ?>
            
            <span class="commenter-rating"><ion-icon name="star"></ion-icon><?php the_field(\'commenter_rating_number\', $comment); ?></span>
            
        <?php endif; ?>
    
    <div class="comment-meta commentmetadata"><?php edit_comment_link(__(\'(Edit)\'),\'  \',\'\' );
        ?>
    </div>

    <div class="reply">
    <?php comment_reply_link(array_merge( $args, array(\'add_below\' => $add_below, \'depth\' => $depth, \'max_depth\' => $args[\'max_depth\']))) ?>
    </div>
    <?php if ( \'div\' != $args[\'style\'] ) : ?>
    </div>
    <?php endif; ?>
    <div class="separator"></div>
<?php }
最后,这是在主题本身(注释)中输出它的代码。php文件:

<?php comment_form(array(
    \'title_reply\' => \'\',
    // Redefine your own textarea (the comment body).
        \'comment_field\' => \'<p class="comment-form-comment"><label for="comment">\' . _x( \'Comments\', \'noun\' ) . \'</label><textarea id="comment" name="comment" aria-required="true" placeholder="What are your thoughts?"></textarea></p>\',
)); ?>



<?php if (have_comments()) : ?>

    <h5><?php comments_number(); ?></h5>

    <ul class="comment-list">
        <?php wp_list_comments(\'type=comment&callback=html5blankcomments\'); // Custom callback in functions.php ?>
    </ul>

<?php elseif ( ! comments_open() && ! is_page() && post_type_supports( get_post_type(), \'comments\' ) ) : ?>

    <p><?php _e( \'Comments are closed here.\', \'html5blank\' ); ?></p>

<?php endif; ?>
有什么想法吗?我正在查看正在传递的变量,所有内容似乎都是正确的,但由于这些错误,我觉得我可能忽略了像这样简单的事情。

非常感谢您的帮助。

1 个回复
SO网友:kstach001

我想出来了。而不是在我的函数中尝试将这两个函数组合在一起。php文件,我添加了这两个文件。为什么会这样?因为一个函数是添加新注释(通过调用ajax comments.php),另一个函数是查询所有现有注释。所以是这样的:

我的原创作品html5blankcomments() 函数获取了现有的WP注释

  • misha_submit_ajax_comment() 函数(您可以在我在问题中发布的教程链接中看到)与ajax注释对话。php文件,只需提交(和显示)new 注释html5blankcomments()) 以及新的评论misha_submit_ajax_comment() 显示时不刷新页面