2019独角兽企业重金招聘Python工程师标准>>>
之所以将这篇教程放前面,是因为前面两节教程刚讲了过滤器和钩子,所以这篇文章就作为wordpress过滤器的一个实例来看,这篇教程的用途:修改评论表单样式,删除评论表单前面或后面的多余内容,给评论表单添加内容。前面wordpress主题制作基础教程之制作评论模板我们添加表单使用了wordpress提供的一个函数comment_form();该函数位于wp-includes/comment-template.php文件,函数介绍:
- comment_form( $args, $post_id );
- //参数$args是一个数组,用来配置表单的一些显示内容
- //$post_id为评论表单对应的文章ID,默认为当前文章ID。
- ?>
一、修改表单配置
对于数组参数$args到底有哪些呢?我们看到comment_form函数的源码中,在定义一个数组$defaults的后面有一行代码
- $args = wp_parse_args( $args, apply_filters( 'comment_form_defaults', $defaults ) );
这行代码是函数中第一次出现参数$args的地方,也就是将传入的数组参数跟数组$defaults比较替换,将$args中的元素去替换$defaults中对应键的元素,所以只要是$defaults中出现了的元素,$args就可以有:
- $defaults = array(
- 'fields' => apply_filters( 'comment_form_default_fields', $fields ),
- 'comment_field' => '
class="comment-form-comment">
', - 'must_log_in' => '
class="must-log-in">' . sprintf( __( 'You must be "%s">logged in to post a comment.' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '
', - 'logged_in_as' => '
class="logged-in-as">' . sprintf( __( 'Logged in as "%1$s">%2$s. "%3$s" title="Log out of this account">Log out?' ), admin_url( 'profile.php' ), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '
', - 'comment_notes_before' => '
class="comment-notes">' . __( 'Your email address will not be published.' ) . ( $req ? $required_text : '' ) . '
', - 'comment_notes_after' => '
class="form-allowed-tags">' . sprintf( __( 'You may use these "HyperText Markup Language">HTML tags and attributes: %s' ), '
',' . allowed_tags() . '
' ) . ' - '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' ),
- );
本工作室的评论表单配置如下,就改变了几个很简单的元素:
- $defaults = array(
- 'comment_field' => '
class="comment-form-comment">
', - 'comment_notes_before' => '',
- 'label_submit' => __( '提交评论' ),
- 'comment_notes_after' =>''
- );
- comment_form($defaults);
我将comment_field-也就是评论内容输入的文本域前面的“评论”字样删掉了,然后comment_notes_before为空-也就是那个提醒“您的邮箱地址不会被公开”,然后comment_notes_after也为空-就是评论表单后面那个提示你可以使用哪些标签。
如果你想修改对应的某些项,找到你的主题的comment_form函数(一般来说在comments.php文件),然后看他的参数,自行修改。。。
二、过滤器应用
不过到这里好像跟我说的过滤器实例还没扯上啊,我们看到comment_form在$defaults数组的前面还有一个数组
- $fields = array(
- 'author' => '
class="comment-form-author">' . ' ' . ( $req ? 'class="required">*' : '' ) .
- '"author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' />',
- 'email' => '
class="comment-form-email"> ' . ( $req ? 'class="required">*' : '' ) .
- '"email" name="email" type="text" value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' />',
- 'url' => '
class="comment-form-url">' .
- '"url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" />',
- );
这个数组就是评论表单前面的三个评论者信息输入文本框,我想也有很多人需要修改这个东西,额,实际上这个$fields数组也在$defaults数组中了,$defaults数组的第一个元素就是,不过我们还是要转个弯、多走一步路,以便讲解过滤器的使用。$defaults的第一个元素是:
- 'fields' => apply_filters( 'comment_form_default_fields', $fields ),
这里提供了一个过滤器comment_form_default_fields,修改的参数就是$fields;要修改这个参数,只需要添加一个过滤器,比如:
- add_filter('comment_form_default_fields','my_custom_fields');
- function my_custom_fields($fields){
- $fields = array(
- 'author' => '
class="comment-form-field comment-form-author">' . ' ' . ( $req ? 'class="required">*' : '' ) .
- '"author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="13"' . $aria_req . ' />',
- 'email' => '
class="comment-form-field comment-form-email"> ' . ( $req ? 'class="required">*' : '' ) .
- '"email" name="email" type="text" value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="13"' . $aria_req . ' />',
- 'url' => '
class="comment-form-field comment-form-url">' .
- '"url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="13" />',
- );
- return $fields;
- }
- ?>
注意:过滤器函数必须要有返回值。。。
尽情的查找apply_filters函数,然后尽情的修改吧。。