wordpress3.0之comment_form()实战(上)

发布于,归属于wordpress20个座位已被强势霸占! 共有1,087人围观    

comment_form()是wordpress3.0出现的新的实用方法,作用是快速生成评论表单。comment_form大大简化了评论表单构建过程,从此以后你无须书写一堆html代码产生评论表格,只要一行:

  1. <?php comment_form(); ?>

就搞定问题了!明河借用个广告词“so easy!” -_-!

comment_form()未出现前

你可以打开你主题的comments.php ,你会看到类似如下的表单代码:

  1. <?php if ( comments_open() ) : ?>
  2.  
  3. <div id="respond">
  4.     <div class="single-box mar-t-10">
  5.          <div class="single-box-header clearfix"><span></span>有话要说?请在此留下阁下高见</div>
  6.     </div>
  7.  
  8.     <div id="cancel-comment-reply">
  9.         <small><?php cancel_comment_reply_link() ?></small>
  10.     </div>
  11.  
  12. <?php if ( get_option('comment_registration') && !is_user_logged_in() ) : ?>
  13. <p><?php printf(__('请先<a href="%s">登录</a>,然后再发表评论', 'kubrick'), wp_login_url( get_permalink() )); ?></p>
  14. <?php else : ?>
  15.  
  16. <form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">
  17. <table width="100%" border="0" cellspacing="0" cellpadding="0" class="comment-table mar-t-10">
  18. <?php if ( is_user_logged_in() ) : ?>
  19.  
  20. <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>
  21.  
  22. <?php else : ?>
  23.   <tr>
  24.     <td><label for="author"><?php _e('用户名', 'kubrick'); ?> <?php if ($req) _e("(必须)", "kubrick"); ?></label></td>
  25.     <td><label for="email"><?php _e('邮箱 (不会公开', 'kubrick'); ?> <?php if ($req) _e(",必须)", "kubrick"); ?></label></td>
  26.     <td><label for="url"><?php _e('站点URL', 'kubrick'); ?></label></td>
  27.   </tr>
  28.   <tr>
  29.     <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>
  30.     <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>
  31.     <td><input type="text" name="url" id="url" value="<?php echo  esc_attr($comment_author_url); ?>" size="25" tabindex="3" class="comment-input" />
  32. </td>
  33.   </tr>
  34. <?php endif; ?>
  35.   <tr>
  36.     <td colspan="3">你的评论</td>
  37.   </tr>
  38.   <tr>
  39.      <td colspan="3"><textarea name="comment" id="comment" cols="58" rows="10" tabindex="4" class="comment-input" ></textarea></td>
  40.   </tr>
  41.   <tr>
  42.     <td><input name="submit" type="submit" id="submit" class="comment-submit" tabindex="5" value="<?php _e('提交评论', 'kubrick'); ?>" /><?php comment_id_fields(); ?></td>
  43.     <td>&nbsp;</td>
  44.     <td></td>
  45.   </tr>
  46. </table>
  47. <?php do_action('comment_form', $post->ID); ?>
  48.  
  49. </form>
  50.  
  51. <?php endif; // If registration required and not logged in ?>

以上代码来自明河自制的RIA之家主题,大部分的评论的表单都与之类似,是不是觉得有些头晕?

comment_form()出现之后

  1. <?php comment_form(); ?>

一行搞定!这里明河说明下,wordpress3.0的默认模板依旧采用旧的表单生成形式,而wordpress3.0新加入的twentyten已经使用comment_form()来生成评论表单了。
实际上还需要做一些处理,比如评论中按钮和lable是英文,设置中文,还需要comment_form的参数帮忙

comment_form()参数说明

comment_form()有二个参数

  1. <?php comment_form($args, $post_id); ?>
  • $args:comment_form()的输出配置参数,为一个关联数组,配置项非常丰富,将再下一步说明。
  • $post_id:文章id,默认为空,即当前id

comment_form()的$args参数详解

$args的默认配置项如下:

  1. $defaults = array(
  2.         'fields'               => apply_filters( 'comment_form_default_fields', $fields ),
  3.         '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>',
  4.         '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>',
  5.         '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>',
  6.         'comment_notes_before' => '<p class="comment-notes">' . __( 'Your email address will not be published.' ) . ( $req ? $required_text : '' ) . '</p>',
  7.         '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>',
  8.         'id_form'              => 'commentform',
  9.         'id_submit'            => 'submit',
  10.         'title_reply'          => __( 'Leave a Reply' ),
  11.         'title_reply_to'       => __( 'Leave a Reply to %s' ),
  12.         'cancel_reply_link'    => __( 'Cancel reply' ),
  13.         'label_submit'         => __( 'Post Comment' ),
  14.     );

配置项很多,明河以评论表单中的英文改中文为例:

  1. $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>',
  2.                            '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>',
  3.                            'comment_notes_before' => '<p class="comment-notes">' . __( '你的email不会被公开。' ) . ( $req ? $required_text : '' ) . '</p>',
  4.                            'comment_notes_after'  => '<p class="form-allowed-tags">' . sprintf( __( '你只能使用以下 <abbr title="HyperText Markup Language">HTML</abbr> 标签和属性: %s' ), ' <code>' . allowed_tags() . '</code>' ) . '</p>',
  5.                            'title_reply'          => __( '留下一个回复' ),
  6.                            'title_reply_to'       => __( '给%s的回复' ),
  7.                            'cancel_reply_link'    => __( '取消回复' ),
  8.                            'label_submit'         => __( '提交评论' ));
  9. comment_form($comment_form_args);


可以看到表单已经全部汉化了。但是这样只适用于一个主题,如果我希望所有的主题(有应用该函数)都是汉化的,如何处理呢。请看下一步解说

修改comment_form()的$args默认参数

1、请打开“wp-includes”,再打开“comment-template.php”。
2、找到comment_form函数。
3、找到“$defaults”变量,将其覆盖成:

  1. $defaults = array(
  2.         'fields'               => apply_filters( 'comment_form_default_fields', $fields ),
  3.         '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>',
  4.         '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>',
  5.         '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>',
  6.         'comment_notes_before' => '<p class="comment-notes">' . __( '你的email不会被公开。' ) . ( $req ? $required_text : '' ) . '</p>',
  7.         'comment_notes_after'  => '<p class="form-allowed-tags">' . sprintf( __( '你只能使用以下 <abbr title="HyperText Markup Language">HTML</abbr> 标签和属性: %s' ), ' <code>' . allowed_tags() . '</code>' ) . '</p>',
  8.         'id_form'              => 'commentform',
  9.         'id_submit'            => 'submit',
  10.         'title_reply'          => __( '留下一个回复' ),
  11.         'title_reply_to'       => __( '给%s的回复' ),
  12.         'cancel_reply_link'    => __( '取消回复' ),
  13.         'label_submit'         => __( '提交评论' ),
  14.     );

还有一个地方要修改的,查找“$required_text”,修改此变量如下:

  1. $required_text = sprintf( ' ' . __('带%s号为必填选项'), '<span class="required">*</span>' );

保存,完毕!

明河语:comment_form()远比想象中的强大,下一篇教程将讲解自定义表单字段和该函数的钩子用法。

(如果您喜欢这篇教程,可以通过支付宝打赏我们1元哦,拜谢!)

跟作者说两句

:wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :?: :-| :-x :-o :-P :-? :) :( :!: 8-O 8)

20个座位已被强势霸占!

  1. CH5iRe

    很好学习了,twentyten默认的98%给我改掉了,奸笑ing

  2. guny

    好是好,只是这样以后他是如何处理皮肤(skin or 表单布局)的呢?

    明河共影回复于 2010年08月29日 9:09

    呵呵,有办法处理的,主要通过调整comment_form()的参数还有其样式。下一篇教程讲解

  3. keke

    这个默认的twentyten的主题的评论在IE(6、7、8)浏览器中都显示变形了,不知道怎么解决好,嗨郁闷

  4. 阿修

    想添加个“欢迎回来,xxx”的效果,搞了半天没成功……哎

    明河共影回复于 2010年09月11日 8:05

    呵呵,要用到comment_form()的钩子,请看明河的下篇教程。

  5. weight

    hi again

    Sky

    gui回复于 2010年12月10日 10:49

    @weight, ceshi

  6. iceorr

    comment-template.php文件输入中文的话,全部是乱码啊,怎么解决?修改编码方式再上传会崩溃.

    明河共影回复于 2010年10月25日 5:52

    ?你的主题编码不是utf-8吗?。使用记事本修改编码。

    iceorr回复于 2010年10月26日 6:06

    @明河共影,
    谢谢,已经解决

  7. 定语

    呃,字体颜色怎么改呢?
    因为评论区域的字体颜色和背景颜色是一样的,所以我在CSS里把commentform的字体颜色改掉了,但是链接的颜色还是和之前一样(就是登录之后显示用户名的地方)。。。我想问下CSS里那个链接的标签是什么?

  8. in1874

    这个很有用,最近刚在改了一个就主题,拿走 试试。

  9. Albert

    你好!按照方法修改后,为什么评论标题和textarae对不齐呀?

    明河共影回复于 2011年01月26日 9:53

    这是样式问题,需要自己调整的哦。comment_form()只管表单输出,样式要自己处理。

  10. Suitear

    悲剧~看中下,还得找~ :(

  11. UC浏览器下载

    学习拉!!!

  12. 最火视频

    学习啦,多谢博主

  13. 猪头博客

    太强大啦 、学习了

  14. icebone

    灰常好,正在搞这块,nice love you!