comment_form()是wordpress3.0出现的新的实用方法,作用是快速生成评论表单。comment_form大大简化了评论表单构建过程,从此以后你无须书写一堆html代码产生评论表格,只要一行:
- <?php comment_form(); ?>
就搞定问题了!明河借用个广告词“so easy!” -_-!
comment_form()未出现前
你可以打开你主题的comments.php ,你会看到类似如下的表单代码:
- <?php if ( comments_open() ) : ?>
- <div id="respond">
- <div class="single-box mar-t-10">
- <div class="single-box-header clearfix"><span></span>有话要说?请在此留下阁下高见</div>
- </div>
- <div id="cancel-comment-reply">
- <small><?php cancel_comment_reply_link() ?></small>
- </div>
- <?php if ( get_option('comment_registration') && !is_user_logged_in() ) : ?>
- <p><?php printf(__('请先<a href="%s">登录</a>,然后再发表评论', 'kubrick'), wp_login_url( get_permalink() )); ?></p>
- <?php else : ?>
- <form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">
- <table width="100%" border="0" cellspacing="0" cellpadding="0" class="comment-table mar-t-10">
- <?php if ( is_user_logged_in() ) : ?>
- <p class="mar-t-b-10"><?php printf(__('你的用户名是<a href="%1$s">%2$s</a>', 'kubrick'), get_option('siteurl') . '/wp-admin/profile.php', $user_identity); ?> ,<a href="<?php echo wp_logout_url(get_permalink()); ?>" title="<?php _e('Log out of this account', 'kubrick'); ?>"><?php _e('注销;', 'kubrick'); ?></a></p>
- <?php else : ?>
- <tr>
- <td><label for="author"><?php _e('用户名', 'kubrick'); ?> <?php if ($req) _e("(必须)", "kubrick"); ?></label></td>
- <td><label for="email"><?php _e('邮箱 (不会公开', 'kubrick'); ?> <?php if ($req) _e(",必须)", "kubrick"); ?></label></td>
- <td><label for="url"><?php _e('站点URL', 'kubrick'); ?></label></td>
- </tr>
- <tr>
- <td><input type="text" name="author" id="author" value="<?php echo esc_attr($comment_author); ?>" size="25" tabindex="1" <?php if ($req) echo "aria-required='true'"; ?> class="comment-input" /></td>
- <td><input type="text" name="email" id="email" value="<?php echo esc_attr($comment_author_email); ?>" size="25" tabindex="2" <?php if ($req) echo "aria-required='true'"; ?> class="comment-input" /></td>
- <td><input type="text" name="url" id="url" value="<?php echo esc_attr($comment_author_url); ?>" size="25" tabindex="3" class="comment-input" />
- </td>
- </tr>
- <?php endif; ?>
- <tr>
- <td colspan="3">你的评论</td>
- </tr>
- <tr>
- <td colspan="3"><textarea name="comment" id="comment" cols="58" rows="10" tabindex="4" class="comment-input" ></textarea></td>
- </tr>
- <tr>
- <td><input name="submit" type="submit" id="submit" class="comment-submit" tabindex="5" value="<?php _e('提交评论', 'kubrick'); ?>" /><?php comment_id_fields(); ?></td>
- <td> </td>
- <td></td>
- </tr>
- </table>
- <?php do_action('comment_form', $post->ID); ?>
- </form>
- <?php endif; // If registration required and not logged in ?>
以上代码来自明河自制的RIA之家主题,大部分的评论的表单都与之类似,是不是觉得有些头晕?
comment_form()出现之后
- <?php comment_form(); ?>
一行搞定!这里明河说明下,wordpress3.0的默认模板依旧采用旧的表单生成形式,而wordpress3.0新加入的twentyten已经使用comment_form()来生成评论表单了。
实际上还需要做一些处理,比如评论中按钮和lable是英文,设置中文,还需要comment_form的参数帮忙
comment_form()参数说明
comment_form()有二个参数
- <?php comment_form($args, $post_id); ?>
- $args:comment_form()的输出配置参数,为一个关联数组,配置项非常丰富,将再下一步说明。
- $post_id:文章id,默认为空,即当前id
comment_form()的$args参数详解
$args的默认配置项如下:
- $defaults = array(
- 'fields' => apply_filters( 'comment_form_default_fields', $fields ),
- 'comment_field' => '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) . '</label><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>',
- 'must_log_in' => '<p class="must-log-in">' . sprintf( __( 'You must be <a href="%s">logged in</a> to post a comment.' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>',
- 'logged_in_as' => '<p class="logged-in-as">' . sprintf( __( 'Logged in as <a href="%1$s">%2$s</a>. <a href="%3$s" title="Log out of this account">Log out?</a>' ), admin_url( 'profile.php' ), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>',
- 'comment_notes_before' => '<p class="comment-notes">' . __( 'Your email address will not be published.' ) . ( $req ? $required_text : '' ) . '</p>',
- 'comment_notes_after' => '<p class="form-allowed-tags">' . sprintf( __( 'You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: %s' ), ' <code>' . allowed_tags() . '</code>' ) . '</p>',
- 'id_form' => 'commentform',
- 'id_submit' => 'submit',
- 'title_reply' => __( 'Leave a Reply' ),
- 'title_reply_to' => __( 'Leave a Reply to %s' ),
- 'cancel_reply_link' => __( 'Cancel reply' ),
- 'label_submit' => __( 'Post Comment' ),
- );
配置项很多,明河以评论表单中的英文改中文为例:
- $comment_form_args = array('must_log_in' => '<p class="must-log-in">' . sprintf( __( '你必须先 <a href="%s">登录</a>才能发表评论。' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>',
- 'logged_in_as' => '<p class="logged-in-as">' . sprintf( __( '<a href="%1$s">%2$s</a>已经登录,要 <a href="%3$s" title="注销用户">注销</a>此用户吗?' ), admin_url( 'profile.php' ), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>',
- 'comment_notes_before' => '<p class="comment-notes">' . __( '你的email不会被公开。' ) . ( $req ? $required_text : '' ) . '</p>',
- 'comment_notes_after' => '<p class="form-allowed-tags">' . sprintf( __( '你只能使用以下 <abbr title="HyperText Markup Language">HTML</abbr> 标签和属性: %s' ), ' <code>' . allowed_tags() . '</code>' ) . '</p>',
- 'title_reply' => __( '留下一个回复' ),
- 'title_reply_to' => __( '给%s的回复' ),
- 'cancel_reply_link' => __( '取消回复' ),
- 'label_submit' => __( '提交评论' ));
- comment_form($comment_form_args);

可以看到表单已经全部汉化了。但是这样只适用于一个主题,如果我希望所有的主题(有应用该函数)都是汉化的,如何处理呢。请看下一步解说
修改comment_form()的$args默认参数
1、请打开“wp-includes”,再打开“comment-template.php”。
2、找到comment_form函数。
3、找到“$defaults”变量,将其覆盖成:
- $defaults = array(
- 'fields' => apply_filters( 'comment_form_default_fields', $fields ),
- 'comment_field' => '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) . '</label><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>',
- 'must_log_in' => '<p class="must-log-in">' . sprintf( __( '你必须先 <a href="%s">登录</a>才能发表评论。' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>',
- 'logged_in_as' => '<p class="logged-in-as">' . sprintf( __( '<a href="%1$s">%2$s</a>已经登录,要 <a href="%3$s" title="注销用户">注销</a>此用户吗?' ), admin_url( 'profile.php' ), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>',
- 'comment_notes_before' => '<p class="comment-notes">' . __( '你的email不会被公开。' ) . ( $req ? $required_text : '' ) . '</p>',
- 'comment_notes_after' => '<p class="form-allowed-tags">' . sprintf( __( '你只能使用以下 <abbr title="HyperText Markup Language">HTML</abbr> 标签和属性: %s' ), ' <code>' . allowed_tags() . '</code>' ) . '</p>',
- 'id_form' => 'commentform',
- 'id_submit' => 'submit',
- 'title_reply' => __( '留下一个回复' ),
- 'title_reply_to' => __( '给%s的回复' ),
- 'cancel_reply_link' => __( '取消回复' ),
- 'label_submit' => __( '提交评论' ),
- );
还有一个地方要修改的,查找“$required_text”,修改此变量如下:
- $required_text = sprintf( ' ' . __('带%s号为必填选项'), '<span class="required">*</span>' );
保存,完毕!
明河语:comment_form()远比想象中的强大,下一篇教程将讲解自定义表单字段和该函数的钩子用法。

很好学习了,twentyten默认的98%给我改掉了,奸笑ing
好是好,只是这样以后他是如何处理皮肤(skin or 表单布局)的呢?
呵呵,有办法处理的,主要通过调整comment_form()的参数还有其样式。下一篇教程讲解
这个默认的twentyten的主题的评论在IE(6、7、8)浏览器中都显示变形了,不知道怎么解决好,嗨郁闷
想添加个“欢迎回来,xxx”的效果,搞了半天没成功……哎
呵呵,要用到comment_form()的钩子,请看明河的下篇教程。
hi again
Sky
@weight, ceshi
comment-template.php文件输入中文的话,全部是乱码啊,怎么解决?修改编码方式再上传会崩溃.
?你的主题编码不是utf-8吗?。使用记事本修改编码。
@明河共影,
谢谢,已经解决
呃,字体颜色怎么改呢?
因为评论区域的字体颜色和背景颜色是一样的,所以我在CSS里把commentform的字体颜色改掉了,但是链接的颜色还是和之前一样(就是登录之后显示用户名的地方)。。。我想问下CSS里那个链接的标签是什么?
这个很有用,最近刚在改了一个就主题,拿走 试试。
你好!按照方法修改后,为什么评论标题和textarae对不齐呀?
这是样式问题,需要自己调整的哦。comment_form()只管表单输出,样式要自己处理。
悲剧~看中下,还得找~
学习拉!!!
学习啦,多谢博主
太强大啦 、学习了
灰常好,正在搞这块,nice love you!