久久精品国产99国产精品免费看_中文成人在线_日本在线播放视频_精品国产一区二区三区在线观看

個性化 WordPress 評論框

三個參考:

https://blog.wpjam.com/article/custom-wordpress-comment-form/

https://www.wpdaxue.com/wordpress-comment_form.html

https://cnzhx.net/blog/altering-wordpress-comment-form/

來源于:

https://blog.wpjam.com/article/custom-wordpress-comment-form/

自定義 WordPress 評論表單和功能實現(xiàn)

WordPress 是一個可以高度自定義的平臺,它提供了很多接口等方便開發(fā)者根據(jù)自己的需求來自定義功能和外表。在一些使用 WordPress 搭建的產(chǎn)品介紹、個人介紹、博客等類型的網(wǎng)站中,評論往往是必不可少的模塊。而評論模塊中,評論表單又是個比較重要的部分,對于某些特殊的需求,我們往往需要修改表單的外表或者增加一些功能(例如讓評論者填寫更多的個人信息),本文就來徹底的講解一下與之相關(guān)的函數(shù)和修改方法。

關(guān)于 WordPress 表單的基礎(chǔ)知識

我們先來了解一下基礎(chǔ)知識。在 WordPress 主題中,使用?comment_form?函數(shù)來生成一個評論表單。通常把評論模塊的代碼寫進單獨的 comments.php 文件中,然后使用?comments_template?這個函數(shù)在 single.php、page.php 等文件底部引用評論模塊。

本文中,使用 twentyeleven 這個官方主題作為演示,因為這個主題寫的很標準,便于后面的自定義操作。我們現(xiàn)在可以打開這個主題下面的 comments.php 文件,瀏覽第 74 行后面??梢钥吹剿苯邮褂昧艘痪?來調(diào)用評論表單。默認的顯示效果如下:

個性化 WordPress 評論框

調(diào)用這個函數(shù),官方會默認生成這樣一個帶有 名字、郵箱、網(wǎng)址、評論框 的標準表單。下面我們就要對它進行各種改造了,強烈建議你開啟這個主題,然后親自修改文件觀看實際效果。

深入了解 comment_form 函數(shù)

comment_form 是可以傳遞一些參數(shù),我們可以通過編寫對應(yīng)的參數(shù)實現(xiàn)表單自定義。你可以打開官方文檔看一下:http://codex.wordpress.org/Function_Reference/comment_form

。這里比較常用的有下面幾個參數(shù):

  • fields – 控制顯示哪幾個表單,默認的是三個:網(wǎng)名(name)、郵箱(email)、網(wǎng)址(url)。
  • comment_notes_before – 在評論表單前面顯示提示信息。
  • comment_notes_after – 在評論表單后面顯示提示信息。
  • title_reply – 這個參數(shù)改變評論表單標題,默認是:Leave a Reply。
  • label_submit – 這個參數(shù)改變評論表單提交按鈕文字,默認是:Post Comment。

我們下面就通過修改這幾個參數(shù)來實現(xiàn)自定義表單。

自定義 WordPress 評論表單的方法

增加、去掉評論表單中的項目,需要使用 fields 參數(shù)。默認的 WordPress 的 fields 參數(shù)的內(nèi)容如下:

$fields =  array('author' => '

' . '' . __( 'Name' ) . ' ' . ( $req ? '*' : '' ) .''comment_author'] ) . '" size="30"' . $aria_req . ' />

','email'  => '

' . __( 'Email' ) . ' ' . ( $req ? '*' : '' ) .''comment_author_email'] ) . '" size="30"' . $aria_req . ' />

','url'    => '

' . __( 'Website' ) . '' .''comment_author_url'] ) . '" size="30" />

',
);

如果你有點 PHP 基礎(chǔ),上面的代碼很容易理解吧,如果我們想去掉“網(wǎng)址”文本框,我們把上面 ‘url’ 的鍵刪掉即可。我們使用下面語句來替換 twentyeleven 主題中 comments.php 文件中第 74 行后面的的調(diào)用表單的函數(shù) comment_form :

$commenter = wp_get_current_commenter();
$req = get_option( 'require_name_email' );
$aria_req = ( $req ? " aria-required='true'" : '' );
$fields =  array('author' => '

' . '' . __( 'Name' ) . ' ' . ( $req ? '*' : '' ) .''comment_author'] ) . '" size="30"' . $aria_req . ' />

','email'  => '

' . __( 'Email' ) . ' ' . ( $req ? '*' : '' ) .''comment_author_email'] ) . '" size="30"' . $aria_req . ' />

',
);

$comments_args = array('fields' =>  $fields
);

comment_form($comments_args);

上面代碼很容易理解,先使用?wp_get_current_commenter?函數(shù)來獲取當前的評論者的一些信息,方便下面調(diào)用。然后生成了一個 fields 變量,內(nèi)容是一個包含 author、email 兩個鍵的數(shù)組,對應(yīng)的鍵值就是評論表單的 HTML 結(jié)構(gòu)。保存刷新一下就可以看到改變了:

個性化 WordPress 評論框

同樣道理,如果想只顯示一個 網(wǎng)名 文本框,你就吧 email 鍵也刪掉。當然,因為 email 文本框是必填的,這樣會導(dǎo)致出現(xiàn)一些問題。上面介紹的幾個常用的參數(shù),跟 fields 參數(shù)的用法類似,下面我們想要改變評論表單標題和發(fā)表按鈕文字的話,可以這樣寫:

$commenter = wp_get_current_commenter();
$req = get_option( 'require_name_email' );
$aria_req = ( $req ? " aria-required='true'" : '' );
$fields =  array('author' => '

' . '' . __( 'Name' ) . ' ' . ( $req ? '*' : '' ) .''comment_author'] ) . '" size="30"' . $aria_req . ' />

','email'  => '

' . __( 'Email' ) . ' ' . ( $req ? '*' : '' ) .''comment_author_email'] ) . '" size="30"' . $aria_req . ' />

',
);

$comments_args = array('fields' =>  $fields,'title_reply'=>'評論一下','label_submit' => '發(fā)射!'
);

comment_form($comments_args);

保存刷新一下,就可以看到咱們修改的文字了:

個性化 WordPress 評論框

要注意的是,如果你的主題是要給別人用的,特別是外國人,為了國際化,修改的內(nèi)容要用 __() 這個函數(shù)包裹,可以方便翻譯,例如:__( ‘發(fā)射!’ ) 。

為表單增加更多文本框

上面說了怎么去掉某個表單中的文本框,如果我覺得表單功能太弱,想要用戶在發(fā)表評論的時候填寫更多的信息呢?我們?nèi)匀皇褂?fields 這個參數(shù)來傳遞。如果想要增加一個新的文本框讓評論者填寫自己所在的地區(qū),我們使用下面這段代碼:

$commenter = wp_get_current_commenter();
$req = get_option( 'require_name_email' );
$aria_req = ( $req ? " aria-required='true'" : '' );
$fields =  array('author' => '

' . '' . __( 'Name' ) . ' ' . ( $req ? '*' : '' ) .''comment_author'] ) . '" size="30"' . $aria_req . ' />

','email'  => '

' . __( 'Email' ) . ' ' . ( $req ? '*' : '' ) .''comment_author_email'] ) . '" size="30"' . $aria_req . ' />

','position' => '

' . __( '地區(qū)' ) . '' .
        '

'
);

$comments_args = array('fields' =>  $fields,'title_reply'=>'評論一下','label_submit' => '發(fā)射!'
);

comment_form($comments_args);

保存刷新之后,就會多出來一個 “地區(qū)” 表單,由于是新建的,所以沒有樣式,你可以編寫一些 CSS 美化一下,這里不再贅述。

個性化 WordPress 評論框

雖然我們可以在這個文本框中填寫信息,但是你點擊發(fā)送之后,不會有任何變化,因為還沒有具體的功能代碼接受你這個新建表單的內(nèi)容。實現(xiàn)這個功能需要用到 comment_post 這個 hook 鉤子。先給出具體代碼:

function add_comment_meta_values($comment_id) {

    if(isset($_POST['position'])) {$position = wp_filter_nohtml_kses($_POST['position']);
        add_comment_meta($comment_id, 'position', $position, false);}

}
add_action ('comment_post', 'add_comment_meta_values', 1);

將上面代碼復(fù)制到 functions.php 文件中即可。上面代碼大體功能就是:在評論內(nèi)容被提交的時候會觸發(fā) comment_post 這個 hook ,使用 add_action 函數(shù)為 comment_post 這個 hook 綁定一個函數(shù),函數(shù)的內(nèi)容就是接收表單中 position 這個文本框的內(nèi)容,然后過濾掉 html 標簽,再使用?add_comment_meta?這個函數(shù)將內(nèi)容插入到數(shù)據(jù)庫中。具體插入到 wp_commentmeta 這個表中,你提交了信息之后,會在這個表中發(fā)現(xiàn)對應(yīng)內(nèi)容

個性化 WordPress 評論框

僅僅存到了數(shù)據(jù)庫中當然不行了,我們還要取出來在評論內(nèi)容中顯示。使用下面代碼可以調(diào)用出來對應(yīng)的內(nèi)容:

 echo "TA 現(xiàn)在在: ".get_comment_meta( $comment->comment_ID, 'position', true ); ?>

在 functions.php 文件的 570 行附近,找到 comment_text 這個函數(shù),在后面插入這句代碼就可以顯示出來了。保存刷新之后就可以看到剛剛輸入的內(nèi)容:

個性化 WordPress 評論框

主要提供了一個思路和基本的方法,拋磚引玉,根據(jù)你的個人需求進行進一步的改造等。

總結(jié)和思維發(fā)散

本文使用 twentyeleven 這個官方主題作為演示是有原因的,因為它的代碼非常規(guī)范、標準。實現(xiàn)這個本文中自定義方法,主題必須使用 comment_form 這個函數(shù)生成表單。我大體看了一下,國外的主題寫的比較規(guī)范,而國內(nèi)的一些主題則不是使用 comment_form 函數(shù)生成的表單,而是直接寫上了表單的 HTML 結(jié)構(gòu),然后插入一些 PHP 函數(shù)。這樣,本文所說的方法肯定就會失效了。

這種方法是通過直接修改主題目錄下面的 comments.php 文件實現(xiàn)的,這樣可能不好管理。WordPress 也提供了對應(yīng) hook 來實現(xiàn)本文的效果,例如前面提到的去掉表單中的某個文本框,可以使用 comment_form_default_fields 這個 hook 來實現(xiàn)。具體代碼請看之前寫過的文章:WordPress 技巧:去掉評論模塊中的網(wǎng)站鏈接表單。其他的可以自行摸索,這樣可以只在 functions.php 中修改方便管理。

了解了基本實現(xiàn)過程,你就可以根據(jù)自己的需求新建表單的文本框等信息了,不要忘了編寫對應(yīng)的 CSS 代碼讓表單更加美觀。

來源于:

https://www.wpdaxue.com/wordpress-comment_form.html

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

調(diào)用 comment_form()

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

<?php comment_form(); ?>

就像我們在官方的主題 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ù)

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

自定義評論表單

刪除表單字段

如果我們想要刪除網(wǎng)址字段,只需要打開主題的?functions.php?文件,添加以下代碼:
add_filter('comment_form_default_fields', 'mytheme_remove_url');
?
function mytheme_remove_url($arg) {$arg['url'] = '';return $arg;
}

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

新增表單字段

假設(shè)我們要添加一個 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');

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

替換默認表單字段

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

默認的這三個字段如下:
$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() 鉤子

評論表單同時還帶了不少鉤子,讓你可以在喜歡的位置添加你想要的內(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
在這里,倡萌只簡單舉一個小例子,在默認字段后面顯示一句話,同樣添加到主題的 functions.php :
function add_my_tips() {
	echo '歡迎踴躍發(fā)言!';
}
// 在默認字段(前面說的姓名、郵箱和網(wǎng)址)的下面添加字段
add_filter('comment_form_after_fields', 'add_my_tips');
// 在已登錄下面添加字段(因為用戶登錄后,是沒有默認上面三個字段的),所以要使用這個鉤子插入內(nèi)容
add_filter('comment_form_logged_in_after', 'add_my_tips');

其他的就靠大家多多實踐了。

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

來源于:

https://cnzhx.net/blog/altering-wordpress-comment-form/

對大多數(shù)人而言,WordPress?的評論框(留言表單)也許沒有多少需要自定義的地方。我恰好想在提交評論按鈕前面加一段話,順便參考別的資料總結(jié)一下。

文中介紹的方法和代碼基于 WordPress 3.3.1,至于以后會不會根據(jù)更新情況修改,這個很難說。如果有修改,會在文后說明。閱讀此文需要對 PHP 有一點點了解,另外還需要知道?WordPress 主題的構(gòu)建方法。嗯,實際上一般用法只需要依葫蘆畫瓢即可。

目錄 Contents

0. 此文涵蓋的內(nèi)容??

文中介紹的方法僅僅是修改 WordPress 默認的評論框內(nèi)容,也就是默認已有定義的那些元素,不會增加新的域或按鈕。僅僅是“修改”,而不是“重建”。所用方法基于 WordPress Codex 中?comment_form??一節(jié)的參數(shù)和 filter。

1. 背景?

文中的方法分為兩種:a. 修改參數(shù); b. 使用 filter。涉及到的文件都在?theme?文件夾中(位于 /wp-content/theme/ 目錄)。每種方法只涉及到一個特定的文件。這樣就不需要修改 WordPress 核心文件了,那樣會很麻煩:每次升級 WordPress 都需要重新修改,而且容易引起錯誤。

前一種方法只涉及到?comments.php?文件。大多數(shù)主題都會有這個文件。雖然并不是一定要有 comments.php 文件,只不過這是 WordPress 推薦的方式。

第二種方法只需要修改 functions.php 文件。同樣的,大多數(shù)主題都會有這個文件。主題自定義的功能都在這個文件中。與上面不同的是,如果你的主題中沒有這個文件,你可以自己創(chuàng)建一個。

2. 評論框簡史?

在版本 3 之前,整個 WordPress 評論框的全部代碼都是在主題的 comments.php 文件中的。這使得修改它非常的方便。不過帶來的問題就是讓主題看起來有些亂。

開發(fā)人員也注意到了這個問題,然后從?WordPrss 3 開始,評論框就被精簡為一個函數(shù)了,直接在主題的相應(yīng)位置調(diào)用此函數(shù):

 comment_form(); ?>

從此,基本上所有的主題都會直接使用此函數(shù)生成默認的評論框,頂多就是在 CSS 樣式定義上各有各的風格。一切變得極其簡潔。

然而問題也隨之而來:如果有人想要改變評論框,那就需要多動動腦子了。

3. 兩種方法?

有兩種方法可以對評論框進行自定義,能完成的任務(wù)也一樣。要采用哪一種方法一方面看你要干什么,另一方面就看個人的喜好了。

注意:方法 2 是我個人非常推薦的方法,會覆蓋掉方法 1。

方法 1: 在 COMMENTS.PHP 中更改?COMMENT_FORM() 調(diào)用?

這個方法用于以下場景是比較合適的:

  • 修改評論框的所有細節(jié)
  • 修改評論框中各字段(field)的標記,包括標簽(label)
  • 其實,要完成想我開頭提到的那種修改目的,這個方法也是很好的

這個方法是通過給通常的評論框函數(shù)調(diào)用

 comment_form(); ?>

增加一些細節(jié)來實現(xiàn)修改的。這些細節(jié)通過參數(shù)來傳遞給該函數(shù)。要改變什么細節(jié),就指定相應(yīng)的參數(shù)。例如:

 comment_form( $args, $post_id ); >

其中,

  • $args:可選,是一個數(shù)組(array()),包含用于評論框的字符串和字段等的配置內(nèi)容,如果不寫該參數(shù),就使用默認的(見下面);
  • $post_id:可選,指定要在 ID 為 $post_id 的文章下產(chǎn)生該評論框,如果不寫該參數(shù),就會使用當前文章的 ID(即在每篇文章下產(chǎn)生該文章的評論框)

以類似下面的形式定義上面的參數(shù) $args 數(shù)組 array() :

$args = array(
        'parameter_name' => 'value',
        'another_parameter' => 'value'
    ));

默認的參數(shù)數(shù)組內(nèi)容如下(via):

$args = array(
	'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' ),
	'comment_field' => '
' . _x( 'Comment', 'noun' ) . '
',
	'must_log_in' => '
' .  sprintf( __( 'You must be logged in to post a comment.' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( ) ) ) ) . '
',
	'logged_in_as' => '
' . sprintf( __( 'Logged in as %2$s. Log out?' ), admin_url( 'profile.php' ), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( ) ) ) ) . '
',
	'comment_notes_before' => '
' . __( 'Your email address will not be published.' ) . ( $req?? $required_text?: '' ) . '
',
	'comment_notes_after' => '
' . sprintf( __( 'You may use these HTML tags and attributes:?%s' ), ' ' . allowed_tags() . '' ) . '
',
	'fields' => apply_filters( 'comment_form_default_fields', array(
		'author' => '
' . '' . __( 'Name', 'domainreference' ) . ' ' . ( $req?? '*'?: '' ) . ''comment_author'] ) . '" size="30"' . $aria_req . ' />
',
		'email' => '
' . __( 'Email', 'domainreference' ) . ' ' . ( $req?? '*'?: '' ) . ''comment_author_email'] ) . '" size="30"' . $aria_req . ' />
',
		'url' => '
' . __( 'Website', 'domainreference' ) . '' . ''comment_author_url'] ) . '" size="30" />
' ) ) );

能夠使用的參數(shù)基本上包括了評論框的各個方面的細節(jié)。WordPress Codex entry on comment_form()?中對可用的參數(shù)有較為全面的介紹,并指明了參數(shù)的默認值(也就是不做修改時的樣子)。

省事的話,也可以不定義參數(shù)數(shù)組 $args = array(…,而是直接將要修改的某個參數(shù)放在數(shù)組里直接調(diào)用。如下面的例子。

例如,默認情況下,在評論框的下面有一行字提示用戶可以在評論時使用某些 HTML 代碼,存儲這些提示文字的參數(shù)是?comment_notes_after。想修改為別的提示,比如指向網(wǎng)站上某個說明頁面的鏈接(如隱私政策頁面),就可以直接將原始函數(shù)調(diào)用改為:

 comment_form(array(
        'comment_notes_after' => '
請訪問我們的 隱私政策 頁面。
'
    ));
?>

再例如,可以通過下面的形式,將默認的評論框標題 Leave a Reply 改寫為別的,甚至可以是中文字符:

 comment_form(array('title_reply'=>'雁過留聲,人過留名')); ?>

如果需要設(shè)置多個參數(shù),需要在每個參數(shù)后面使用英文半角的逗號( , )隔開(最后一個參數(shù)后除外)。

如果要修改評論框的字段(?fields?)就稍微麻煩一些(此類應(yīng)用建議使用下面的方法 2)。需要在上面參數(shù)數(shù)組 array 的里面創(chuàng)建一個字段數(shù)組(下面例子中將其命名為(賦值給) $fields ),然后通過 filter 來調(diào)用它。新數(shù)組 $fields 將被 ‘fields’ 參數(shù)調(diào)用。下面的例子中在 $fields 數(shù)組中指定了 author 字段的內(nèi)容(包含了顯示它所需要的所有 HTML 代碼):

 comment_form(array(
        $fields = array(
            'author' => '
' . '' . __( 'Your Name' ) . ' . $aria_req . ' />
'
            );
    ));
?>  

這樣就可以更加精細地控制評論框中“作者”(author)的顯示方式。上例中將默認的標簽 “Name” 改成了 “Your Name”,并將默認值(value)設(shè)置成了“Your First and Last Name”?,F(xiàn)在只需要在參數(shù)數(shù)組 array 中調(diào)用該字段定義即可:

 comment_form(array(
        $fields = array(
            'author' => '
' . '' . __( 'Your Name' ) . ' . $aria_req . ' />
'
            );

        'fields'              => apply_filters( 'comment_form_default_fields', $fields ), 
        'comment_notes_after' => '
請訪問我們的 隱私政策 頁面。
'
    ));
?>

其中的一句:

apply_filters( 'comment_form_default_fields', $fields )

就是告訴 WordPress 要使用你提供的代碼(這里使用 $fields 變量來定義)來替換評論框的默認字段 comment_form_default_fields。需要注意的是,上面的代碼中 $fields 里只定義了 author 字段,所以如果你將上面的代碼應(yīng)用到自己的主題的話,評論框顯示出來就只有“姓名”那個欄目了。當然,你也可以比照這個例子繼續(xù)編寫其它字段,email(郵件地址)和 url(網(wǎng)站地址)的代碼。

這里給個直接修改參數(shù)形式的,稍作修改的評論框三個字段的修改代碼,其中按照 HTML5 的建議增加了?placeholder

comment_form(
    array(
        'fields' => array(
            'author' => '<p class="comment-form-author"><label for="author">昵稱label> <span class="required">*span><input type="text" placeholder="姓名或昵稱" aria-required="true" size="30" value="' . esc_attr( $commenter['comment_author'] ) . '" name="author" id="author">p>',
            'email' => '<p class="comment-form-email"><label for="email">郵箱label> <span class="required">*span><input type="text" placeholder="電子郵件地址" aria-required="true" size="30" value="' . esc_attr( $commenter['comment_author_email'] ) . '" name="email" id="email">p>',
            'url' => '<p class="comment-form-url"><label for="url">站點label><input type="text" placeholder="個人主頁網(wǎng)址" size="30" value="'.$comment_author_url.'" name="url" id="url">p>'
            )
        )
 );

方法 2: 在 FUNCTIONS.PHP 中使用 HOOK FILTER?

這種方法比較適合于

  • 省略或改寫字段(姓名、郵件、網(wǎng)址)
  • 當你想使用更加貼近核心也是最徹底的方法的時候

該方法采用 WordPress 的濾器(filters)來實現(xiàn)。使用 filter,可以在評論框最后輸出之前對其進行修改(所以會覆蓋掉在 comments.php 中的修改)。

這個就需要在主題的?functions.php?文件中操作了。將編寫的代碼放到 functions.php 文件的結(jié)尾即可(如果結(jié)尾有 ?>,則應(yīng)放在 ?> 之前)。

如果你的主題沒有 functions.php 文件,自己創(chuàng)建一個就行了(簡單的 Windows 記事本程序就可以操作了),但是要注意的是,自己創(chuàng)建的時候應(yīng)該先在該文檔中輸入下面的代碼做為開頭,

 

然后另起一行輸入自定義的代碼。先創(chuàng)建一個函數(shù)(function)來定義需要進行的操作。不過似乎只能用來操作(修改顯示方式等)字段(fields),比如三個輸入內(nèi)容 author(姓名)、email(電郵)和 url(網(wǎng)址)。

例如,移除某字段:

add_filter('comment_form_default_fields', 'mytheme_remove_commentform_fields');
function mytheme_remove_commentform_fields($fields){
    $fields['email'] = '';  // 后面的參數(shù)留空表示移除 email 字段
    $fields['url'] = '';  // 移除 website 字段return $fields;
}

程序的名稱就隨意啦,只要保證上面一行 add_filter 中調(diào)用的與之一致就行了。

再比如修改 author 字段的形式:

function alter_comment_form_fields($fields){
    $fields['author'] = '
' . '' . __( 'Your name, please' ) . ' ' . ( $req ? '*' : '' ) .
                    ''comment_author'] ) . '" size="30"' . $aria_req . ' />
';
    $fields['email'] = '';  // 移除 email 字段
    $fields['url'] = '';  // 移除 website 字段

    return $fields;
}

需要注意的是,如果要修改某個字段,必須將該字段相關(guān)的所有部分 —— 標簽、輸入框等 —— 都定義在里面,否則……自己看看就知道了。

實際上也可以通過掛鉤 filter 來增加新的字段。只不過,如果沒有相應(yīng)的 WordPress 數(shù)據(jù)庫操作,新加的字段并不會存儲到數(shù)據(jù)庫中,只能是個擺設(shè)。新加字段的方法是:

function my_fields($fields) {
     $fields['new'] = '
新字段' ' . ( $req ? '"required">*' : '' ) . '"new" name="new" size="30" type="text" value="' . esc_attr( $commenter['comment_author_new'] ) . '" />
'; 
    return $fields; 
}
add_filter('comment_form_default_fields','my_fields');

這里給個采用 filter 方式的,稍作修改的評論框三個字段的修改代碼,與前面參數(shù)傳遞方式那個實現(xiàn)的操作是一模一樣的,只不過這個要用在 functions.php 文件里頭。

function alter_comment_form_fields($fields){
    $fields['author'] = > '<p class="comment-form-author"><label for="author">昵稱label> <span class="required">*span><input type="text" placeholder="姓名或昵稱" aria-required="true" size="30" value="' . esc_attr( $commenter['comment_author'] ) . '" name="author" id="author">p>';
    $fields['email'] = > '<p class="comment-form-email"><label for="email">郵箱label> <span class="required">*span><input type="text" placeholder="電子郵件地址" aria-required="true" size="30" value="' . esc_attr( $commenter['comment_author_email'] ) . '" name="email" id="email">p>';
    $fields['url'] = > '<p class="comment-form-url"><label for="url">站點label><input type="text" placeholder="個人主頁網(wǎng)址" size="30" value="'.$comment_author_url.'" name="url" id="url">p>';
    return $fields;
}

add_filter('comment_form_default_fields','alter_comment_form_fields');

直接將上面的代碼放到你的 functions.php 文件中,稍作修改即可。不過,如果要改變評論框出現(xiàn)的位置,那還是需要動一下 comments.php 文件的。

實際上,將下面的代碼保存下來,隨時可以往里面填寫東西來實現(xiàn)修改(每行前面的 // 表示注釋掉改行,即讓它不起作用,所以,要用的話就把 // 刪掉):

function alter_comment_form_fields($fields){
    //$fields['author'] = ''; //removes name field
    //$fields['email'] = '';  //removes email field
    //$fields['url'] = '';  //removes website field
    return $fields;
}

add_filter('comment_form_default_fields','alter_comment_form_fields');

后記?

寫這個的時候查了查資料,參考了2篇文章:

  1. http://chipcullen.com/altering-the-comment-form-in-wordpress/
  2. http://www.1stwebdesigner.com/wordpress/comment-form-customization/

如果你有什么建議和疑問,請在下方留言。?

本文發(fā)表于水景一頁。永久鏈接:<https://cnzhx.net/blog/altering-wordpress-comment-form/>。轉(zhuǎn)載請保留此信息及相應(yīng)鏈接。

給TA贊賞
共{{data.count}}人
人已贊賞
??
Npcink上的部份代碼及教程來源于互聯(lián)網(wǎng),僅供網(wǎng)友學習交流,若您喜歡本文可附上原文鏈接隨意轉(zhuǎn)載。
無意侵害您的權(quán)益,請發(fā)送郵件至 1355471563#qq.com 或點擊右側(cè) 私信:Muze 反饋,我們將盡快處理。
?
購物車
優(yōu)惠劵
搜索
久久精品国产99国产精品免费看_中文成人在线_日本在线播放视频_精品国产一区二区三区在线观看

    伊人久久综合| 久久综合伊人| 欧美日韩国产bt| 精品999日本| 久久精品天堂| 国产偷国产偷亚洲高清97cao| 在线视频精品一区| 欧美日韩免费一区| 99ri日韩精品视频| 欧美人与禽性xxxxx杂性| 亚洲激情av| 欧美成人亚洲成人| 亚洲人成啪啪网站| 欧美激情乱人伦| 亚洲激情国产精品| 欧美黄色aaaa| 99热在线精品观看| 欧美视频在线观看一区| 99re热这里只有精品视频| 欧美激情一区二区三区高清视频| 最新日韩精品| 欧美日韩在线视频首页| 亚洲一区二区精品在线| 国产精品一二三视频| 欧美在线视频一区| 有坂深雪在线一区| 欧美成人一品| 一区二区三区久久网| 国产精品美女久久久久久2018 | 久久精品国产第一区二区三区最新章节 | 亚洲国产精品国自产拍av秋霞| 麻豆精品在线视频| 亚洲剧情一区二区| 国产精品红桃| 久久精品一区二区三区四区| 亚洲国产精品一区二区第一页| 欧美激情影院| 亚洲自拍电影| 在线观看精品视频| 欧美色大人视频| 欧美综合国产| 亚洲精品看片| 国产欧美一区二区三区久久| 猫咪成人在线观看| 亚洲一区二区av电影| 国产综合欧美在线看| 欧美激情网友自拍| 欧美一区二区在线看| 亚洲高清一区二| 国产精品免费一区二区三区在线观看 | 在线观看av不卡| 欧美体内谢she精2性欧美| 欧美在线|欧美| 99国产成+人+综合+亚洲欧美| 国产精品亚洲综合一区在线观看 | 最新国产成人av网站网址麻豆 | 国产精品国色综合久久| 另类av导航| 午夜在线一区| 亚洲毛片av在线| 狠狠色综合播放一区二区| 欧美日韩一区在线播放| 久久综合九色九九| 亚洲欧美一级二级三级| 日韩视频免费| 亚洲国产欧美日韩另类综合| 国产欧美日韩综合精品二区| 欧美日韩一区二区三区在线观看免| 久久福利一区| 亚洲欧美一区二区原创| 日韩亚洲成人av在线| 亚洲成人在线观看视频| 国产区精品在线观看| 国产精品多人| 欧美人与禽猛交乱配视频| 另类av一区二区| 久久精品国产99精品国产亚洲性色| 一区二区三区成人精品| 亚洲人成在线播放| 在线看视频不卡| 国产在线精品二区| 国产日产欧美一区| 国产毛片一区| 国产精品视频久久| 国产精品久久夜| 国产精品福利在线观看| 欧美日韩在线播放三区四区| 欧美二区在线| 欧美精品一区二区三区久久久竹菊 | 亚洲日本精品国产第一区| 亚洲大片在线观看| 亚洲国产精品va在线看黑人| 永久免费视频成人| 在线观看三级视频欧美| 伊人蜜桃色噜噜激情综合| 红桃视频欧美| 在线观看欧美精品| 在线欧美福利| 最新成人在线| 亚洲理论在线| 中文欧美字幕免费| 亚洲综合视频网| 亚洲免费影视| 性欧美长视频| 久久精品国产亚洲精品| 久久女同互慰一区二区三区| 久久久夜夜夜| 欧美华人在线视频| 欧美日韩一区二区三区四区在线观看 | 亚洲精品国产精品乱码不99按摩| 亚洲国产视频直播| 日韩视频二区| 亚洲一区日韩| 久久久久国产精品人| 老色鬼精品视频在线观看播放| 免费在线亚洲欧美| 欧美日韩视频在线| 国产精品一区二区久激情瑜伽| 国产日韩欧美在线播放不卡| 激情久久久久久| 一区二区欧美国产| 性久久久久久久久| 久久久免费精品视频| 欧美黄色一区二区| 国产精品视频内| 伊人蜜桃色噜噜激情综合| 一本到12不卡视频在线dvd| 午夜精品福利在线观看| 麻豆精品在线视频| 国产精品久久久久久户外露出| 国产视频精品免费播放| 亚洲精品久久久久久久久久久| 亚洲一级在线观看| 久久一区视频| 国产精品第一区| 曰本成人黄色| 亚洲一区二区高清视频| 久久久久久久久蜜桃| 欧美日韩综合不卡| 在线不卡a资源高清| 亚洲一区成人| 欧美激情一区在线| 国产一区激情| 亚洲欧美综合| 欧美日韩国产麻豆| 一区二区视频免费在线观看| 亚洲私拍自拍| 欧美激情久久久| 怡红院精品视频| 欧美一区午夜视频在线观看| 欧美日韩亚洲一区二区三区四区 | 欧美日韩直播| 亚洲高清三级视频| 久久国产天堂福利天堂| 欧美吻胸吃奶大尺度电影| 亚洲激情视频在线| 久久久久欧美精品| 国产精品一区二区三区乱码| 99re6这里只有精品视频在线观看| 久久亚洲一区| 国产一区二区黄色| 亚洲综合丁香| 国产精品99免费看 | 美女脱光内衣内裤视频久久网站| 国产精品视频网址| 亚洲视频精品| 欧美午夜精品久久久久久浪潮| 亚洲老板91色精品久久| 欧美jizz19hd性欧美| 一区视频在线播放| 久久婷婷久久| 亚洲成人在线观看视频| 久久综合婷婷| 亚洲国产精品999| 久久一二三国产| 在线观看亚洲视频| 久久综合色婷婷| 1769国内精品视频在线播放| 久久久福利视频| 一区在线播放| 欧美freesex8一10精品| 亚洲激情啪啪| 欧美日韩精品久久| 国产精品99久久不卡二区| 欧美视频在线观看 亚洲欧| 一本色道88久久加勒比精品| 欧美日韩国产91| 亚洲一区二区综合| 国产精品亚洲不卡a| 久久福利影视| 亚洲激情在线| 欧美三级日韩三级国产三级| 一本色道久久综合狠狠躁篇的优点| 欧美日韩亚洲一区二区三区四区 | 亚洲免费网站| 国产一区高清视频| 免费欧美日韩国产三级电影| 夜夜夜精品看看| 国产农村妇女毛片精品久久莱园子| 羞羞答答国产精品www一本|