WEB主題公園
WordPress原創(chuàng)主題,高端網(wǎng)站模板建站,網(wǎng)站模板建站資源以及開(kāi)發(fā)知識(shí)分享,盡在WEB主題公園
WEB主題公園致力于開(kāi)發(fā)適合中國(guó)人習(xí)慣的中文WordPress網(wǎng)站模板,并提供全程視頻教程,讓您能夠輕松的使用網(wǎng)站模板建立好自己的網(wǎng)站!
自定義 WordPress 表單功能
WordPress 是一個(gè)可以高度自定義的平臺(tái),自定義的參數(shù)可以幫助我們非常方便的定制自己的樣式和功能,今天我們就來(lái)詳細(xì)解說(shuō)一下wordpress的表單提交功能,也就是通過(guò)修改wordpress自帶的評(píng)論表單,來(lái)實(shí)現(xiàn)自己的所需要的表單,話(huà)不多說(shuō),我們就來(lái)詳細(xì)的說(shuō)一說(shuō)把!
在 WordPress中,使用 comment_form 函數(shù)來(lái)生成一個(gè)評(píng)論表單。 comments.php 文件中書(shū)寫(xiě)了評(píng)論表單的各種函數(shù),然后使用 comments_template 這個(gè)函數(shù)在 各個(gè)文件中調(diào)用評(píng)論表單。
使用 twentyeleven 這個(gè)官方主題作為演示,我們現(xiàn)在可以打開(kāi)這個(gè)主題下面的 comments.php 文件,我們可以看到官方默認(rèn)生成這樣一個(gè)帶有 名字、郵箱、網(wǎng)址、評(píng)論框 的標(biāo)準(zhǔn)表單。下面我們就要對(duì)它進(jìn)行各種改造了,強(qiáng)烈建議你開(kāi)啟這個(gè)主題,然后親自修改文件觀看實(shí)際效果。
默認(rèn)的表單:

comment_form 可以傳遞一些參數(shù),我們看一下這些常用的參數(shù):
fields – 控制顯示哪幾個(gè)表單,默認(rèn)的是三個(gè):網(wǎng)名(name)、郵箱(email)、網(wǎng)址(url)。
comment_notes_before – 在評(píng)論表單前面顯示提示信息。
comment_notes_after – 在評(píng)論表單后面顯示提示信息。
title_reply – 這個(gè)參數(shù)改變?cè)u(píng)論表單標(biāo)題,默認(rèn)是:Leave a Reply。
label_submit – 這個(gè)參數(shù)改變?cè)u(píng)論表單提交按鈕文字,默認(rèn)是:Post Comment。
下面就通過(guò)修改這幾個(gè)參數(shù)來(lái)實(shí)現(xiàn)我們的定制表單:
增加、去掉評(píng)論表單中的項(xiàng)目,需要使用 fields 參數(shù):
$fields = array(
'author' => '<p>' . '<label for="author">' . __( 'Name' ) . '</label> ' . ( $req ? '<span>*</span>' : '' ) .
'<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>',
'email' => '<p><label for="email">' . __( 'Email' ) . '</label> ' . ( $req ? '<span>*</span>' : '' ) .
'<input id="email" name="email" type="text" value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /></p>',
'url' => '<p><label for="url">' . __( 'Website' ) . '</label>' .
'<input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" /></p>',
);
有一些php基礎(chǔ)的朋友,上面的代碼應(yīng)該不難理解,如果我們想去掉“網(wǎng)址”文本框,我們把上面 ‘url’ 的鍵刪掉即可。我們使用下面語(yǔ)句來(lái)替換 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' => '<p>' . '<label for="author">' . __( 'Name' ) . '</label> ' . ( $req ? '<span>*</span>' : '' ) .
'<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>',
'email' => '<p><label for="email">' . __( 'Email' ) . '</label> ' . ( $req ? '<span>*</span>' : '' ) .
'<input id="email" name="email" type="text" value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /></p>',
);
$comments_args = array(
'fields' => $fields
);
comment_form($comments_args);
上面代碼很容易理解,先使用 wp_get_current_commenter 函數(shù)來(lái)獲取當(dāng)前的評(píng)論者的一些信息,方便下面調(diào)用。然后生成了一個(gè) fields 變量,內(nèi)容是一個(gè)包含 author、email 兩個(gè)鍵的數(shù)組,對(duì)應(yīng)的鍵值就是評(píng)論表單的 HTML 結(jié)構(gòu)。保存刷新一下就可以看到改變了:

同樣道理,如果想只顯示一個(gè) 網(wǎng)名 文本框,你就吧 email 鍵也刪掉。當(dāng)然,因?yàn)?email 文本框是必填的,這樣會(huì)導(dǎo)致出現(xiàn)一些問(wèn)題。上面介紹的幾個(gè)常用的參數(shù),跟 fields 參數(shù)的用法類(lèi)似,下面我們想要改變?cè)u(píng)論表單標(biāo)題和發(fā)表按鈕文字的話(huà),可以這樣寫(xiě):
$commenter = wp_get_current_commenter();
$req = get_option( 'require_name_email' );
$aria_req = ( $req ? " aria-required='true'" : '' );
$fields = array(
'author' => '<p>' . '<label for="author">' . __( 'Name' ) . '</label> ' . ( $req ? '<span>*</span>' : '' ) .
'<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>',
'email' => '<p><label for="email">' . __( 'Email' ) . '</label> ' . ( $req ? '<span>*</span>' : '' ) .
'<input id="email" name="email" type="text" value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /></p>',
);
$comments_args = array(
'fields' => $fields,
'title_reply'=>'評(píng)論一下',
'label_submit' => '發(fā)射!'
);
comment_form($comments_args);保存刷新一下,就可以看到咱們修改的文字了:

要注意的是,如果你的主題是要給別人用的,特別是外國(guó)人,為了國(guó)際化,修改的內(nèi)容要用 __() 這個(gè)函數(shù)包裹,可以方便翻譯,例如:__( ‘發(fā)射!’ ) 。
為表單增加更多文本框
上面說(shuō)了怎么去掉某個(gè)表單中的文本框,如果我覺(jué)得表單功能太弱,想要用戶(hù)在發(fā)表評(píng)論的時(shí)候填寫(xiě)更多的信息呢?我們?nèi)匀皇褂?fields 這個(gè)參數(shù)來(lái)傳遞。如果想要增加一個(gè)新的文本框讓評(píng)論者填寫(xiě)自己所在的地區(qū),我們使用下面這段代碼:
$commenter = wp_get_current_commenter(); $req = get_option( 'require_name_email' ); $aria_req = ( $req ? " aria-required='true'" : '' ); $fields = array( 'author' => '<p>' . '<label for="author">' . __( 'Name' ) . '</label> ' . ( $req ? '<span>*</span>' : '' ) . '<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>', 'email' => '<p><label for="email">' . __( 'Email' ) . '</label> ' . ( $req ? '<span>*</span>' : '' ) . '<input id="email" name="email" type="text" value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /></p>', 'position' => '<p><label for="position">' . __( '地區(qū)' ) . '</label>' . '<input id="position" name="position" type="text" size="30" /></p>' ); $comments_args = array( 'fields' => $fields, 'title_reply'=>'評(píng)論一下', 'label_submit' => '發(fā)射!' ); comment_form($comments_args);保存刷新之后,就會(huì)多出來(lái)一個(gè) “地區(qū)” 表單,由于是新建的,所以沒(méi)有樣式,你可以編寫(xiě)一些 CSS 美化一下,這里不再贅述。
雖然我們可以在這個(gè)文本框中填寫(xiě)信息,但是你點(diǎn)擊發(fā)送之后,不會(huì)有任何變化,因?yàn)檫€沒(méi)有具體的功能代碼接受你這個(gè)新建表單的內(nèi)容。實(shí)現(xiàn)這個(gè)功能需要用到 comment_post 這個(gè) 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 文件中即可。上面代碼大體功能就是:在評(píng)論內(nèi)容被提交的時(shí)候會(huì)觸發(fā) comment_post 這個(gè) hook ,使用 add_action 函數(shù)為 comment_post 這個(gè) hook 綁定一個(gè)函數(shù),函數(shù)的內(nèi)容就是接收表單中 position 這個(gè)文本框的內(nèi)容,然后過(guò)濾掉 html 標(biāo)簽,再使用 add_comment_meta 這個(gè)函數(shù)將內(nèi)容插入到數(shù)據(jù)庫(kù)中。具體插入到 wp_commentmeta 這個(gè)表中,你提交了信息之后,會(huì)在這個(gè)表中發(fā)現(xiàn)對(duì)應(yīng)內(nèi)容

僅僅存到了數(shù)據(jù)庫(kù)中當(dāng)然不行了,我們還要取出來(lái)在評(píng)論內(nèi)容中顯示。使用下面代碼可以調(diào)用出來(lái)對(duì)應(yīng)的內(nèi)容:
<?php echo "TA 現(xiàn)在在: ".get_comment_meta( $comment->comment_ID, 'position', true ); ?>這種方法是通過(guò)直接修改主題目錄下面的 comments.php 文件實(shí)現(xiàn)的,這樣可能不好管理。WordPress 也提供了對(duì)應(yīng) hook 來(lái)實(shí)現(xiàn)本文的效果,例如前面提到的去掉表單中的某個(gè)文本框,可以使用 comment_form_default_fields 這個(gè) hook 來(lái)實(shí)現(xiàn)。具體代碼請(qǐng)看之前寫(xiě)過(guò)的文章:WordPress 技巧:去掉評(píng)論模塊中的網(wǎng)站鏈接表單。其他的可以自行摸索,這樣可以只在 functions.php 中修改方便管理。
在 functions.php 文件的 570 行附近,找到 comment_text 這個(gè)函數(shù),在后面插入這句代碼就可以顯示出來(lái)了。保存刷新之后就可以看到剛剛輸入的內(nèi)容: 
如未標(biāo)明出處,所有文章均為WEB主題公園原創(chuàng),如需轉(zhuǎn)載,請(qǐng)附上原文地址,感謝您的支持和關(guān)注。
本文地址:http://www.dengyin90.cn/zdywordpressbdgn.html
推薦閱讀
- 2017年1月22日
- 2017年10月30日
- 2015年6月16日
- 2017年1月13日
- 2018年3月21日
- 2014年4月25日
- 2016年10月9日
- 2017年2月21日





