欢迎光临
我们一直在努力

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

建站超值云服务器,限时71元/月

本文目录
[隐藏]

  • 1调用 comment_form()
  • 2comment_form() 参数
  • 3自定义评论表单
    • 3.1删除表单字段
    • 3.2新增表单字段
    • 3.3替换默认表单字段
  • 4comment_form() 钩子

WordPress 3.0 新增了comment_form() 函数来构建评论表单,下面简单讲解一下 comment_form() 的使用方法,希望能帮助大家自定义评论表单。

调用 comment_form()

如果你要在主题中调用评论表单,只需要在使用下面简单的代码即可:

1
<?php comment_form(); ?>

<?php comment_form(); ?>

就像我们在官方的主题 twentyfourteen 的 comments.php 文件的倒数第2行看到一样:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
/**
 * The template for displaying Comments
 *
 * The area of the page that contains comments and the comment form.
 *
 * @package WordPress
 * @subpackage Twenty_Fourteen
 * @since Twenty Fourteen 1.0
 */
/*
 * If the current post is protected by a password and the visitor has not yet
 * entered the password we will return early without loading the comments.
 */
if ( post_password_required() ) {
	return;
}
?>
<div id="comments" class="comments-area">
	<?php if ( have_comments() ) : ?>
	<h2 class="comments-title">
		<?php
			printf( _n( 'One thought on “%2$s”', '%1$s thoughts on “%2$s”', get_comments_number(), 'twentyfourteen' ),
				number_format_i18n( get_comments_number() ), get_the_title() );
		?>
	</h2>
	<?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : ?>
	<nav id="comment-nav-above" class="navigation comment-navigation" role="navigation">
		<h1 class="screen-reader-text"><?php _e( 'Comment navigation', 'twentyfourteen' ); ?></h1>
		<div class="nav-previous"><?php previous_comments_link( __( '← Older Comments', 'twentyfourteen' ) ); ?></div>
		<div class="nav-next"><?php next_comments_link( __( 'Newer Comments →', 'twentyfourteen' ) ); ?></div>
	</nav><!-- #comment-nav-above -->
	<?php endif; // Check for comment navigation. ?>
	<ol class="comment-list">
		<?php
			wp_list_comments( array(
				'style'      => 'ol',
				'short_ping' => true,
				'avatar_size'=> 34,
			) );
		?>
	</ol><!-- .comment-list -->
	<?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : ?>
	<nav id="comment-nav-below" class="navigation comment-navigation" role="navigation">
		<h1 class="screen-reader-text"><?php _e( 'Comment navigation', 'twentyfourteen' ); ?></h1>
		<div class="nav-previous"><?php previous_comments_link( __( '← Older Comments', 'twentyfourteen' ) ); ?></div>
		<div class="nav-next"><?php next_comments_link( __( 'Newer Comments →', 'twentyfourteen' ) ); ?></div>
	</nav><!-- #comment-nav-below -->
	<?php endif; // Check for comment navigation. ?>
	<?php if ( ! comments_open() ) : ?>
	<p class="no-comments"><?php _e( 'Comments are closed.', 'twentyfourteen' ); ?></p>
	<?php endif; ?>
	<?php endif; // have_comments() ?>
	<?php comment_form(); ?>
</div><!-- #comments -->

<?php /** * The template for displaying Comments * * The area of the page that contains comments and the comment form. * * @package WordPress * @subpackage Twenty_Fourteen * @since Twenty Fourteen 1.0 */ /* * If the current post is protected by a password and the visitor has not yet * entered the password we will return early without loading the comments. */ if ( post_password_required() ) { return; } ?> <div id=”comments” class=”comments-area”> <?php if ( have_comments() ) : ?> <h2 class=”comments-title”> <?php printf( _n( ‘One thought on “%2$s”’, ‘%1$s thoughts on “%2$s”’, get_comments_number(), ‘twentyfourteen’ ), number_format_i18n( get_comments_number() ), get_the_title() ); ?> </h2> <?php if ( get_comment_pages_count() > 1 && get_option( ‘page_comments’ ) ) : ?> <nav id=”comment-nav-above” class=”navigation comment-navigation” role=”navigation”> <h1 class=”screen-reader-text”><?php _e( ‘Comment navigation’, ‘twentyfourteen’ ); ?></h1> <div class=”nav-previous”><?php previous_comments_link( __( ‘← Older Comments’, ‘twentyfourteen’ ) ); ?></div> <div class=”nav-next”><?php next_comments_link( __( ‘Newer Comments →’, ‘twentyfourteen’ ) ); ?></div> </nav><!– #comment-nav-above –> <?php endif; // Check for comment navigation. ?> <ol class=”comment-list”> <?php wp_list_comments( array( ‘style’ => ‘ol’, ‘short_ping’ => true, ‘avatar_size’=> 34, ) ); ?> </ol><!– .comment-list –> <?php if ( get_comment_pages_count() > 1 && get_option( ‘page_comments’ ) ) : ?> <nav id=”comment-nav-below” class=”navigation comment-navigation” role=”navigation”> <h1 class=”screen-reader-text”><?php _e( ‘Comment navigation’, ‘twentyfourteen’ ); ?></h1> <div class=”nav-previous”><?php previous_comments_link( __( ‘← Older Comments’, ‘twentyfourteen’ ) ); ?></div> <div class=”nav-next”><?php next_comments_link( __( ‘Newer Comments →’, ‘twentyfourteen’ ) ); ?></div> </nav><!– #comment-nav-below –> <?php endif; // Check for comment navigation. ?> <?php if ( ! comments_open() ) : ?> <p class=”no-comments”><?php _e( ‘Comments are closed.’, ‘twentyfourteen’ ); ?></p> <?php endif; ?> <?php endif; // have_comments() ?> <?php comment_form(); ?> </div><!– #comments –>

comment_form() 参数

1
<?php comment_form($args, $post_id); ?>

<?php comment_form($args, $post_id); ?>

  • $args:comment_form() 的输出配置参数,为一个关联数组,配置项非常丰富,下面我们会详细说明。
  • $post_id:文章id,默认为空,即当前id
  • $args的默认配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$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' ),
    );

$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’ ), );

自定义评论表单

删除表单字段

如果我们想要删除网址字段,只需要打开主题的 functions.php 文件,添加以下代码:

1
2
3
4
5
6
add_filter('comment_form_default_fields', 'mytheme_remove_url');
 
function mytheme_remove_url($arg) {
    $arg['url'] = '';
    return $arg;
}

add_filter(‘comment_form_default_fields’, ‘mytheme_remove_url’); function mytheme_remove_url($arg) { $arg[‘url’] = ”; return $arg; }

保存后刷新页面,你就会看到“url”输入框已经不存在了。

新增表单字段

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

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

function my_fields($fields) { $fields[‘qq’] = ‘<p class=”comment-form-qq”>’ . ‘<label for=”qq”>’.__(‘QQ’).'</label> ‘ . ‘<input id=”qq” name=”qq” type=”text” value=”‘ . esc_attr( $commenter[‘comment_qq’] ) . ‘” size=”30″ /></p>’; return $fields; } add_filter(‘comment_form_default_fields’,’my_fields’);

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

替换默认表单字段

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

默认的这三个字段如下:

1
2
3
4
5
6
7
8
$fields =  array(
	'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>',
	'email'  => '<p class="comment-form-email"><label for="email">' . __( 'Email' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) .
	'<input id="email" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /></p>',
	'url'    => '<p class="comment-form-url"><label for="url">' . __( 'Website' ) . '</label>' .
	'<input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" /></p>',
	);

$fields = array( ‘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>’, ’email’ => ‘<p class=”comment-form-email”><label for=”email”>’ . __( ‘Email’ ) . ‘</label> ‘ . ( $req ? ‘<span class=”required”>*</span>’ : ” ) . ‘<input id=”email” name=”email” type=”text” value=”‘ . esc_attr( $commenter[‘comment_author_email’] ) . ‘” size=”30″‘ . $aria_req . ‘ /></p>’, ‘url’ => ‘<p class=”comment-form-url”><label for=”url”>’ . __( ‘Website’ ) . ‘</label>’ . ‘<input id=”url” name=”url” type=”text” value=”‘ . esc_attr( $commenter[‘comment_author_url’] ) . ‘” size=”30″ /></p>’, );

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

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

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

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 通过 comment_form() 自定义 WordPress 评论表单
分享到: 更多 (0)