wordpress功能集成(四)改变评论框样式

更新:2017-07-17

之所以将这篇教程放前面,是因为前面两节教程刚讲了过滤器和钩子,所以这篇文章就作为wordpress过滤器的一个实例来看,这篇教程的用途:修改评论表单样式,删除评论表单前面或后面的多余内容,给评论表单添加内容。前面wordpress主题制作基础教程之制作评论模板我们添加表单使用了wordpress提供的一个函数comment_form();该函数位于wp-includes/comment-template.php文件,函数介绍:

  1. <?php
  2. comment_form( $args$post_id );
  3. //参数$args是一个数组,用来配置表单的一些显示内容
  4. //$post_id为评论表单对应的文章ID,默认为当前文章ID。
  5. ?>

一、修改表单配置

对于数组参数$args到底有哪些呢?我们看到comment_form函数的源码中,在定义一个数组$defaults的后面有一行代码

  1. $args = wp_parse_args( $args, apply_filters( 'comment_form_defaults', $defaults ) );

这行代码是函数中第一次出现参数$args的地方,也就是将传入的数组参数跟数组$defaults比较替换,将$args中的元素去替换$defaults中对应键的元素,所以只要是$defaults中出现了的元素,$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. $defaults = array(
  2.     'comment_field'        => '<p class="comment-form-comment"><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>',
  3.     'comment_notes_before' => '',
  4.     'label_submit'         => __( '提交评论' ),
  5.     'comment_notes_after' =>''
  6. );
  7. comment_form($defaults);

我将comment_field-也就是评论内容输入的文本域前面的“评论”字样删掉了,然后comment_notes_before为空-也就是那个提醒“您的邮箱地址不会被公开”,然后comment_notes_after也为空-就是评论表单后面那个提示你可以使用哪些标签。

 

 

如果你想修改对应的某些项,找到你的主题的comment_form函数(一般来说在comments.php文件),然后看他的参数,自行修改。。。

二、过滤器应用

不过到这里好像跟我说的过滤器实例还没扯上啊,我们看到comment_form在$defaults数组的前面还有一个数组

  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.     );

这个数组就是评论表单前面的三个评论者信息输入文本框,我想也有很多人需要修改这个东西,额,实际上这个$fields数组也在$defaults数组中了,$defaults数组的第一个元素就是,不过我们还是要转个弯、多走一步路,以便讲解过滤器的使用。$defaults的第一个元素是:

  1. 'fields' => apply_filters( 'comment_form_default_fields', $fields ),

这里提供了一个过滤器comment_form_default_fields,修改的参数就是$fields;要修改这个参数,只需要添加一个过滤器,比如:

2017-07-17更新:特别注意,很多新入门朋友会犯的错误,即先从comment_form函数源码中复制代码,然后用过滤器,在这个过滤器中,请先将复制出来的变量删除,比如源码中的:

  1. //复制的author中含有 __()函数,$req $commenter变量等 
  2. 'author' => '<p class="comment-form-author">' . '<label for="author">' . __( 'Name' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) . '<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>',

正确的做法是像下面代码中,不含变量。

  1. add_filter('comment_form_default_fields','ashuwp_custom_comments_fields');
  2. function ashuwp_custom_comments_fields( $fields ){
  3.   $fields =  array(
  4.     'author' => '<p class="comment-form-field comment-form-author"><label for="author">称呼*</label><span class="required">*</span><input id="author" name="author" type="text" value="" /></p>',
  5.     'email' => '<p class="comment-form-field comment-form-email"><label for="email">Email*</label><span class="required">*</span><input id="email" name="email" type="text" value="" /></p>',
  6.     'url' => '<p class="comment-form-field comment-form-url"><label for="url">网址</label><input id="url" name="url" type="text" value="" /></p>',
  7.     );
  8.     return $fields;
  9. }

注意:过滤器函数必须要有返回值。。。

尽情的查找apply_filters函数,然后尽情的修改吧。。

已有19条评论

阿树工作室进行回复 取消回复