當(dāng)前位置:首頁>WordPress建站>WordPress開發(fā)>通過 comment_form() 自定義 WordPress 評(píng)論表單

通過 comment_form() 自定義 WordPress 評(píng)論表單

WordPress 3.0 新增了comment_form() 函數(shù)來構(gòu)建評(píng)論表單,下面簡單講解一下 comment_form() 的使用方法,希望能幫助大家自定義評(píng)論表單。

調(diào)用 comment_form()

如果你要在主題中調(diào)用評(píng)論表單,只需要在使用下面簡單的代碼即可:

<?php comment_form(); ?>

就像我們?cè)诠俜降闹黝} twentyfourteen 的 comments.php 文件的倒數(shù)第2行看到一樣:

<?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() 參數(shù)

<?php comment_form($args, $post_id); ?>
  • $args:comment_form() 的輸出配置參數(shù),為一個(gè)關(guān)聯(lián)數(shù)組,配置項(xiàng)非常豐富,下面我們會(huì)詳細(xì)說明。
  • $post_id:文章id,默認(rèn)為空,即當(dāng)前id
  • $args的默認(rèn)配置:
$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' ),
    );

自定義評(píng)論表單

刪除表單字段

如果我們想要?jiǎng)h除網(wǎng)址字段,只需要打開主題的 functions.php 文件,添加以下代碼:

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

保存后刷新頁面,你就會(huì)看到“url”輸入框已經(jīng)不存在了。

新增表單字段

假設(shè)我們要添加一個(gè) QQ 字段,同樣在主題的 functions.php 添加下面的代碼即可:

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

刷新頁面,即可看到新增的表單。

替換默認(rèn)表單字段

代碼和上面的例子差不多,如果你設(shè)置的字段為(author、email、url)其中之一,即 $fields[‘author’]、$fields[’email’]、$fields[‘url’] ,就可以替換默認(rèn)的字段的輸出內(nèi)容。

默認(rèn)的這三個(gè)字段如下:

$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() 鉤子

評(píng)論表單同時(shí)還帶了不少鉤子,讓你可以在喜歡的位置添加你想要的內(nèi)容,具體鉤子如下:

  • 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

在這里,倡萌只簡單舉一個(gè)小例子,在默認(rèn)字段后面顯示一句話,同樣添加到主題的 functions.php :

function add_my_tips() {
	echo '歡迎踴躍發(fā)言!';
}
// 在默認(rèn)字段(前面說的姓名、郵箱和網(wǎng)址)的下面添加字段
add_filter('comment_form_after_fields', 'add_my_tips');
// 在已登錄下面添加字段(因?yàn)橛脩舻卿浐螅菦]有默認(rèn)上面三個(gè)字段的),所以要使用這個(gè)鉤子插入內(nèi)容
add_filter('comment_form_logged_in_after', 'add_my_tips');

其他的就靠大家多多實(shí)踐了。

更多信息,請(qǐng)參考官方文檔:http://codex.wordpress.org/Function_Reference/comment_form

還可以看下《圖解WordPress評(píng)論表單鉤子

聲明:本站所有文章,如無特殊說明或標(biāo)注,均為本站原創(chuàng)發(fā)布。任何個(gè)人或組織,在未征得本站同意時(shí),禁止復(fù)制、盜用、采集、發(fā)布本站內(nèi)容到任何網(wǎng)站、書籍等各類媒體平臺(tái)。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。

給TA打賞
共{{data.count}}人
人已打賞
歡迎關(guān)注WordPress大學(xué)公眾號(hào) WPDAXUE
WordPress開發(fā)

如何使用 WordPress 的拾色器(Color Picker)API

2014-5-6 9:45:04

WordPress開發(fā)

WordPress 功能相近的兩個(gè)插件互相沖突的解決辦法

2014-5-14 15:45:15

10 條回復(fù) A文章作者 M管理員
  1. 江北

    新增的表單字段好像沒有添加到數(shù)據(jù)庫里面

  2. 這個(gè)應(yīng)該不用讓用戶看見信息

  3. flippy

    比如增加新字段,如何讀取這個(gè)字段的內(nèi)容呀?求倡萌叔指教

  4. 感謝講解。我有些疑問就是,我單獨(dú)處理update_comment_meta和get_comment_meta函數(shù)為什么不成功。我想通過異步方式添加評(píng)論點(diǎn)贊功能。但是這兩個(gè)函數(shù)就是不成功

  5. 嘗試一下

  6. 學(xué)習(xí)了

  7. ?? 很好用,最近正打算改一下WordPress自帶的主題。就是不知道怎么在評(píng)論框前面把表情加上去。現(xiàn)在可以了。

  8. 這樣子的添加的表單,用戶后臺(tái)還是看不到這個(gè)信息,所以不是很完美

?
個(gè)人中心
購物車
優(yōu)惠劵
今日簽到
有新私信 私信列表
搜索

泰顺县| 永新县| 城市| 巩留县| 台湾省| 柯坪县| 阿尔山市| 三原县| 湾仔区| 盖州市| 樟树市| 广昌县| 乳山市| 互助| 东港市| 库伦旗| 杭锦后旗| 丹阳市| 齐齐哈尔市| 龙陵县| 普宁市| 湘潭县| 邵阳县| 梁山县| 漳平市| 土默特左旗| 贡嘎县| 沐川县| 寿光市| 勃利县| 宣武区| 岑巩县| 宁陵县| 吴江市| 新乐市| 申扎县| 文登市| 太保市| 全州县| 高邮市| 永寿县|