请选择 进入手机版 | 继续访问电脑版

Java321技术网

 找回密码
 立即注册
搜索
热搜: centos
查看: 7695|回复: 0

通过 comment_form() 自定义 WordPress 评论表单

[复制链接]

39

主题

42

帖子

439

积分

超级版主

Rank: 8Rank: 8

积分
439
发表于 2017-7-5 04:07:56 | 显示全部楼层 |阅读模式
WordPress 3.0 新增了comment_form() 函数来构建评论表单,下面简单讲解一下 comment_form() 的使用方法,希望能帮助大家自定义评论表单。


  1. <?php comment_form(); ?>
复制代码
comment_form() 参数

  1. <?php comment_form($args, $post_id); ?>
复制代码
  • $args:comment_form() 的输出配置参数,为一个关联数组,配置项非常丰富,下面我们会详细说明。
  • $post_id:文章id,默认为空,即当前id
  • $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. add_filter('comment_form_default_fields', 'mytheme_remove_url');

  2. function mytheme_remove_url($arg) {
  3.     $arg['url'] = '';
  4.     return $arg;
  5. }
复制代码
保存后刷新页面,你就会看到“url”输入框已经不存在了。


新增表单字段

假设我们要添加一个 QQ 字段,同样在主题的 functions.php 添加下面的代码即可:

  1. function my_fields($fields) {
  2.         $fields['qq'] = '<p class="comment-form-qq">' . '<label for="qq">'.__('QQ').'</label> ' .
  3.         '<input id="qq" name="qq" type="text" value="' . esc_attr( $commenter['comment_qq'] ) . '" size="30" /></p>';
  4.         return $fields;
  5. }
  6. add_filter('comment_form_default_fields','my_fields');
复制代码

刷新页面,即可看到新增的表单。

替换默认表单字段

代码和上面的例子差不多,如果你设置的字段为(author、email、url)其中之一,即 $fields[‘author’]、$fields[’email’]、$fields[‘url’] ,就可以替换默认的字段的输出内容。

默认的这三个字段如下:


  1. $fields =  array(
  2.         'author' => '<p class="comment-form-author">' . '<label for="author">' . __( 'Name' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) .
  3.         '<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>',
  4.         'email'  => '<p class="comment-form-email"><label for="email">' . __( 'Email' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) .
  5.         '<input id="email" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /></p>',
  6.         'url'    => '<p class="comment-form-url"><label for="url">' . __( 'Website' ) . '</label>' .
  7.         '<input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" /></p>',
  8.         );
复制代码
comment_form() 钩子

评论表单同时还带了不少钩子,让你可以在喜欢的位置添加你想要的内容,具体钩子如下:


  • comment_form_before
  • comment_form_must_log_in_after
  • comment_form_top
  • comment_form_logged_in_after
  • comment_notes_before
  • comment_form_before_fields
  • comment_form_field_{$name}
  • comment_form_after_fields
  • comment_form_field_comment
  • comment_form (action hook)
  • comment_form_after
  • comment_form_comments_closed

在这里,倡萌只简单举一个小例子,在默认字段后面显示一句话,同样添加到主题的 functions.php :

  1. function add_my_tips() {
  2.         echo '欢迎踊跃发言!';
  3. }
  4. // 在默认字段(前面说的姓名、邮箱和网址)的下面添加字段
  5. add_filter('comment_form_after_fields', 'add_my_tips');
  6. // 在已登录下面添加字段(因为用户登录后,是没有默认上面三个字段的),所以要使用这个钩子插入内容
  7. add_filter('comment_form_logged_in_after', 'add_my_tips');
复制代码

其他的就靠大家多多实践了。


回复

使用道具 举报

QQ|Archiver|手机版|小黑屋|Java321技术网   蜀ICP备15030946号-1

GMT+8, 2024-3-29 06:18 , Processed in 0.058961 second(s), 22 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表