Wordpress评论表单未提交

时间:2015-09-18 08:06:39

标签: php ajax wordpress

我有一个我无法解决的问题。在我的single.php页面上,我添加了评论表单:

<?php comments_template('/partials/comments.php'); ?>

在我的partials文件夹中,我创建了一个comments.php(来自主题,其工作正常):

<?php
/**
 * The template for displaying Comments.
 *
 * The area of the page that contains both current comments
 * and the comment form. The actual display of comments is
 * handled by a callback to MYtheme_comment().
 *
 */


 function MYtheme_comment( $comment, $args, $depth ) {
    $GLOBALS['comment'] = $comment;
    switch ( $comment->comment_type ) :
        case 'pingback' :
        case 'trackback' :
            ?>
            <li class="post pingback">
                <p><?php esc_html_e( 'Pingback:', 'my_theme' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( esc_html__( 'Edit', 'my_theme' ), '<span class="edit-link">', '</span>' ); ?></p>
            <?php
            break;
        default :
            ?>
            <li <?php comment_class('clearfix'); ?> id="comment-<?php comment_ID(); ?>">
                <span class="comment_avatar">
                <?php
                $avatar_size = 100;
                if ( '0' != $comment->comment_parent ){
                    $avatar_size = 100;
                }

                echo get_avatar( $comment, $avatar_size );
                echo '</span><span class="comment_content">';
                ?>

                <div class="comment-text">

                <?php
                /* translators: 1: comment author, 2: date and time */
                printf( esc_html__( '%1$s  %2$s', 'my_theme' ),
                    sprintf( '<span class="comment-author">%s</span>', get_comment_author_link() ),
                    sprintf( '<time pubdate datetime="%2$s">%3$s</time>',
                        esc_url( get_comment_link( $comment->comment_ID ) ),
                        get_comment_time( 'c' ),
                        /* translators: 1: date, 2: time */
                        sprintf( esc_html__( '%1$s at %2$s', 'my_theme' ), get_comment_date(), get_comment_time() )
                    )
                );
                ?>

                <?php edit_comment_link( esc_html__( 'Edit', 'my_theme' ), '<span class="edit-link">', '</span>' ); ?>
                <?php if ( $comment->comment_approved == '0' ) : ?>
                    <em class="comment-awaiting-moderation"><?php esc_html_e( 'Your comment is awaiting moderation.', 'my_theme' ); ?></em>
                    <br />
                <?php else: ?>
                <?php comment_text(); ?>
                <?php endif; ?>
                <p class="reply">
                    <?php comment_reply_link( array_merge( $args, array( 'reply_text' => esc_html__( 'Reply ', 'my_theme' ).'<i class="ci_icon-chevronright-thin"></i>', 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
                </p><!-- .reply -->
                    </span>
                </div>

            <?php
            break;
        endswitch;
}


?>
    <div id="comments">
    <?php if ( post_password_required() ) : ?>
        <p class="nopassword"><?php esc_html_e( 'This post is password protected. Enter the password to view any comments.', 'my_theme' ); ?></p>
    </div><!-- #comments -->
    <?php
            /* Stop the rest of comments.php from being processed,
             * but don't kill the script entirely -- we still have
             * to fully load the template.
             */
            return;
        endif;
    ?>

    <?php // You can start editing here -- including this comment! ?>

    <?php if ( have_comments() ) : ?>
        <h3 id="comments-title"><?php printf( _n( 'One comment', '%1$s comments', get_comments_number(), 'my_theme' ), number_format_i18n( get_comments_number() ) );?></h3>

        <?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : // are there comments to navigate through ?>
        <nav id="comment-nav-above">
            <h1 class="assistive-text"><?php esc_html_e( 'Comment navigation', 'my_theme' ); ?></h1>
            <div class="nav-previous"><?php previous_comments_link( esc_html__( '&larr; Older Comments', 'my_theme' ) ); ?></div>
            <div class="nav-next"><?php next_comments_link( esc_html__( 'Newer Comments &rarr;', 'my_theme' ) ); ?></div>
        </nav>
        <?php endif; // check for comment navigation ?>

        <ol class="commentlist">
            <?php
                /* Loop through and list the comments. Tell wp_list_comments()
                 * to use MYtheme_comment() to format the comments.
                 * If you want to overload this in a child theme then you can
                 * define MYtheme_comment() and that will be used instead.
                 * See MYtheme_comment() in twentyeleven/functions.php for more.
                 */

                wp_list_comments( array( 'callback' => 'MYtheme_comment' ) );
            ?>
        </ol>

        <?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : // are there comments to navigate through ?>
        <nav id="comment-nav-below">
            <h1 class="assistive-text"><?php esc_html_e( 'Comment navigation', 'my_theme' ); ?></h1>
            <div class="nav-previous"><?php previous_comments_link( esc_html__( '&larr; Older Comments', 'my_theme' ) ); ?></div>
            <div class="nav-next"><?php next_comments_link( esc_html__( 'Newer Comments &rarr;', 'my_theme' ) ); ?></div>
        </nav>
        <?php endif; // check for comment navigation ?>

    <?php
        /* If there are no comments and comments are closed, let's leave a little note, shall we?
         * But we don't want the note on pages or post types that do not support comments.
         */
        elseif ( ! comments_open() && ! is_page() && post_type_supports( get_post_type(), 'comments' ) ) :
    ?>
        <p class="nocomments"><?php esc_html_e( 'Comments are closed.', 'my_theme' ); ?></p>
    <?php endif; ?>

    <?php

    $commenter = wp_get_current_commenter();
    $req = get_option( 'require_name_email' );
    $aria_req = ( $req ? " aria-required='true'" : '' );

    $fields =  array(
      'author' =>
        '<div class="comment_fields"><p class="comment-form-author"><input id="author" name="author" type="text" placeholder="' . esc_html__( 'Name*', 'my_theme' ) . '" value="' . esc_attr( $commenter['comment_author'] ) .
        '" size="30"' . $aria_req . ' /></p>',
      'email' =>
        '<p class="comment-form-email"><input id="email" name="email" type="text" placeholder="' . esc_html__( 'E-mail*', 'my_theme' ) . '" value="' . esc_attr(  $commenter['comment_author_email'] ) .
        '" size="30"' . $aria_req . ' /></p>',
      'url' =>
        '<p class="comment-form-url"><input id="url" name="url" type="text" placeholder="' . esc_html__( 'Website*', 'my_theme' ) . '" value="' . esc_attr(  $commenter['comment_author_url'] ) .
        '" size="30"' . $aria_req . ' /></p></div>',
    );

    $comment_field = '<p class="comment-form-comment"><textarea id="comment" name="comment" placeholder="' . esc_html__( 'Message*', 'my_theme' ) . '" cols="45" rows="8" aria-required="true"></textarea></p>';

    comment_form(array(
        'fields' => $fields,
        'comment_field' => $comment_field,
        'comment_notes_after' => '',
        'id_submit' => 'comment-submit',
        'title_reply' => esc_html__( 'Leave a reply', 'my_theme' ),
        'label_submit' => esc_html__( 'Leave a comment', 'my_theme' ),
    )); ?>

    <div class="clear"></div>

</div><!-- #comments -->

表单出现,我的评论在wordpress中启用。我填写表格并点击提交按钮,没有任何反应!

所以我尝试通过添加here

中的代码来解析它
<script type="text/javascript">
  jQuery('document').ready(function($){
    // Get the comment form
    var commentform = $('#commentform');
    // Add a Comment Status message
    commentform.prepend('<div id="comment-status" ></div>');
    // Defining the Status message element
    var statusdiv = $('#comment-status');
    commentform.submit(function(){
        // Serialize and store form data
        var formdata = commentform.serialize();
        //Add a status message
        statusdiv.html('<p class="ajax-placeholder">Processing...</p>');
        //Extract action URL from commentform
        var formurl = commentform.attr('action');
        //Post Form with data
        $.ajax({
            type: 'post',
            url: formurl,
            data: formdata,
            error: function(XMLHttpRequest, textStatus, errorThrown){
                statusdiv.html('<p class="ajax-error" >You might have left one of the fields blank, or be posting too quickly</p>');
            },
            success: function(data, textStatus){
                if(data == "success")
                    statusdiv.html('<p class="ajax-success" >Thanks for your comment. We appreciate your response.</p>');
                else
                    statusdiv.html('<p class="ajax-error" >Please wait a while before posting your next comment</p>');
                commentform.find('textarea[name=comment]').val('');
            }
        });
        return false;
        });
  });
</script>

现在当我评论时,我首先得到关于将其中一个字段留空的第一个错误,然后立即得到关于过快发布评论的第二个错误。我还注意到Chrome中控制台中出现409 (Conflict)错误来自mythemefolder/wp-comments-post.php,这是来自wordpress的操作链接的默认错误。

奇怪的是,一旦我刷新页面,我就能看到我用ajax帖子添加的帖子。如果我删除ajax并尝试发表评论,然后刷新,则没有任何反应。

那么问题是什么呢?当我没有AJAX时,为什么没有发布任何内容,为什么在我有AJAX但仅在页面刷新时出现评论?

修改

在我检查并发送表格时,在Firebug中,我得到:

POST mythemefolder/wp-comments-post.php 302 Moved Temporarily 2,51s
POST mythemefolder/wp-comments-post.php 409 Conflict 1,37s
GET mythemefolder/postname/#comment-11 200 OK 2,64s

所以有些事情发生了。当我打开冲突时,我找到了

<p>Duplicate comment detected; it looks as though you&#8217;ve already said that!</p></body>

任何线索?

1 个答案:

答案 0 :(得分:0)

找到答案,错误是我的主题。单击时,preventDefault()按钮上有.submit。删除该部分时,评论表格有效。