三個參考:
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 評論表單和功能實現
WordPress 是一個可以高度自定義的平臺,它提供了很多接口等方便開發者根據自己的需求來自定義功能和外表。在一些使用 WordPress 搭建的產品介紹、個人介紹、博客等類型的網站中,評論往往是必不可少的模塊。而評論模塊中,評論表單又是個比較重要的部分,對于某些特殊的需求,我們往往需要修改表單的外表或者增加一些功能(例如讓評論者填寫更多的個人信息),本文就來徹底的講解一下與之相關的函數和修改方法。
關于 WordPress 表單的基礎知識
我們先來了解一下基礎知識。在 WordPress 主題中,使用?comment_form?函數來生成一個評論表單。通常把評論模塊的代碼寫進單獨的 comments.php 文件中,然后使用?comments_template?這個函數在 single.php、page.php 等文件底部引用評論模塊。
本文中,使用 twentyeleven 這個官方主題作為演示,因為這個主題寫的很標準,便于后面的自定義操作。我們現在可以打開這個主題下面的 comments.php 文件,瀏覽第 74 行后面。可以看到它直接使用了一句?來調用評論表單。默認的顯示效果如下:
調用這個函數,官方會默認生成這樣一個帶有 名字、郵箱、網址、評論框 的標準表單。下面我們就要對它進行各種改造了,強烈建議你開啟這個主題,然后親自修改文件觀看實際效果。
深入了解 comment_form 函數
comment_form 是可以傳遞一些參數,我們可以通過編寫對應的參數實現表單自定義。你可以打開官方文檔看一下:http://codex.wordpress.org/Function_Reference/comment_form
。這里比較常用的有下面幾個參數:
- fields – 控制顯示哪幾個表單,默認的是三個:網名(name)、郵箱(email)、網址(url)。
- comment_notes_before – 在評論表單前面顯示提示信息。
- comment_notes_after – 在評論表單后面顯示提示信息。
- title_reply – 這個參數改變評論表單標題,默認是:Leave a Reply。
- label_submit – 這個參數改變評論表單提交按鈕文字,默認是:Post Comment。
我們下面就通過修改這幾個參數來實現自定義表單。
自定義 WordPress 評論表單的方法
增加、去掉評論表單中的項目,需要使用 fields 參數。默認的 WordPress 的 fields 參數的內容如下:
$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 基礎,上面的代碼很容易理解吧,如果我們想去掉“網址”文本框,我們把上面 ‘url’ 的鍵刪掉即可。我們使用下面語句來替換 twentyeleven 主題中 comments.php 文件中第 74 行后面的的調用表單的函數 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?函數來獲取當前的評論者的一些信息,方便下面調用。然后生成了一個 fields 變量,內容是一個包含 author、email 兩個鍵的數組,對應的鍵值就是評論表單的 HTML 結構。保存刷新一下就可以看到改變了:
同樣道理,如果想只顯示一個 網名 文本框,你就吧 email 鍵也刪掉。當然,因為 email 文本框是必填的,這樣會導致出現一些問題。上面介紹的幾個常用的參數,跟 fields 參數的用法類似,下面我們想要改變評論表單標題和發表按鈕文字的話,可以這樣寫:
$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' => '發射!'
);
comment_form($comments_args);
保存刷新一下,就可以看到咱們修改的文字了:
要注意的是,如果你的主題是要給別人用的,特別是外國人,為了國際化,修改的內容要用 __() 這個函數包裹,可以方便翻譯,例如:__( ‘發射!’ ) 。
為表單增加更多文本框
上面說了怎么去掉某個表單中的文本框,如果我覺得表單功能太弱,想要用戶在發表評論的時候填寫更多的信息呢?我們仍然使用 fields 這個參數來傳遞。如果想要增加一個新的文本框讓評論者填寫自己所在的地區,我們使用下面這段代碼:
$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' => '
' . __( '地區' ) . '' .
'
'
);
$comments_args = array('fields' => $fields,'title_reply'=>'評論一下','label_submit' => '發射!'
);
comment_form($comments_args);
保存刷新之后,就會多出來一個 “地區” 表單,由于是新建的,所以沒有樣式,你可以編寫一些 CSS 美化一下,這里不再贅述。
雖然我們可以在這個文本框中填寫信息,但是你點擊發送之后,不會有任何變化,因為還沒有具體的功能代碼接受你這個新建表單的內容。實現這個功能需要用到 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);
將上面代碼復制到 functions.php 文件中即可。上面代碼大體功能就是:在評論內容被提交的時候會觸發 comment_post 這個 hook ,使用 add_action 函數為 comment_post 這個 hook 綁定一個函數,函數的內容就是接收表單中 position 這個文本框的內容,然后過濾掉 html 標簽,再使用?add_comment_meta?這個函數將內容插入到數據庫中。具體插入到 wp_commentmeta 這個表中,你提交了信息之后,會在這個表中發現對應內容
僅僅存到了數據庫中當然不行了,我們還要取出來在評論內容中顯示。使用下面代碼可以調用出來對應的內容:
echo "TA 現在在: ".get_comment_meta( $comment->comment_ID, 'position', true ); ?>
在 functions.php 文件的 570 行附近,找到 comment_text 這個函數,在后面插入這句代碼就可以顯示出來了。保存刷新之后就可以看到剛剛輸入的內容:
主要提供了一個思路和基本的方法,拋磚引玉,根據你的個人需求進行進一步的改造等。
總結和思維發散
本文使用 twentyeleven 這個官方主題作為演示是有原因的,因為它的代碼非常規范、標準。實現這個本文中自定義方法,主題必須使用 comment_form 這個函數生成表單。我大體看了一下,國外的主題寫的比較規范,而國內的一些主題則不是使用 comment_form 函數生成的表單,而是直接寫上了表單的 HTML 結構,然后插入一些 PHP 函數。這樣,本文所說的方法肯定就會失效了。
這種方法是通過直接修改主題目錄下面的 comments.php 文件實現的,這樣可能不好管理。WordPress 也提供了對應 hook 來實現本文的效果,例如前面提到的去掉表單中的某個文本框,可以使用 comment_form_default_fields 這個 hook 來實現。具體代碼請看之前寫過的文章:WordPress 技巧:去掉評論模塊中的網站鏈接表單。其他的可以自行摸索,這樣可以只在 functions.php 中修改方便管理。
了解了基本實現過程,你就可以根據自己的需求新建表單的文本框等信息了,不要忘了編寫對應的 CSS 代碼讓表單更加美觀。
來源于:
https://www.wpdaxue.com/wordpress-comment_form.html
WordPress 3.0 新增了comment_form() 函數來構建評論表單,下面簡單講解一下 comment_form() 的使用方法,希望能幫助大家自定義評論表單。
調用 comment_form()
如果你要在主題中調用評論表單,只需要在使用下面簡單的代碼即可:
<?php comment_form(); ?>
就像我們在官方的主題 twentyfourteen 的 comments.php 文件的倒數第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() 參數
1
<?php comment_form($args, $post_id); ?>
- $args:comment_form() 的輸出配置參數,為一個關聯數組,配置項非常豐富,下面我們會詳細說明。
- $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' ),);
自定義評論表單
刪除表單字段
如果我們想要刪除網址字段,只需要打開主題的?functions.php?文件,添加以下代碼:
add_filter('comment_form_default_fields', 'mytheme_remove_url');
?
function mytheme_remove_url($arg) {$arg['url'] = '';return $arg;
}
保存后刷新頁面,你就會看到“url”輸入框已經不存在了。
新增表單字段
假設我們要添加一個 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');
刷新頁面,即可看到新增的表單。
替換默認表單字段
代碼和上面的例子差不多,如果你設置的字段為(author、email、url)其中之一,即 $fields[‘author’]、$fields[’email’]、$fields[‘url’] ,就可以替換默認的字段的輸出內容。
默認的這三個字段如下:
$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 :
function add_my_tips() {
echo '歡迎踴躍發言!';
}
// 在默認字段(前面說的姓名、郵箱和網址)的下面添加字段
add_filter('comment_form_after_fields', 'add_my_tips');
// 在已登錄下面添加字段(因為用戶登錄后,是沒有默認上面三個字段的),所以要使用這個鉤子插入內容
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/
對大多數人而言,WordPress?的評論框(留言表單)也許沒有多少需要自定義的地方。我恰好想在提交評論按鈕前面加一段話,順便參考別的資料總結一下。
文中介紹的方法和代碼基于 WordPress 3.3.1,至于以后會不會根據更新情況修改,這個很難說。如果有修改,會在文后說明。閱讀此文需要對 PHP 有一點點了解,另外還需要知道?WordPress 主題的構建方法。嗯,實際上一般用法只需要依葫蘆畫瓢即可。
目錄 Contents
- 0. 此文涵蓋的內容
- 1. 背景
- 2. 評論框簡史
- 3. 兩種方法
- 方法 1: 在 comments.php 中更改?COMMENT_FORM() 調用
- 方法 2: 在 functions.php 中使用 HOOK FILTER
- 后記
0. 此文涵蓋的內容??
文中介紹的方法僅僅是修改 WordPress 默認的評論框內容,也就是默認已有定義的那些元素,不會增加新的域或按鈕。僅僅是“修改”,而不是“重建”。所用方法基于 WordPress Codex 中?comment_form??一節的參數和 filter。
1. 背景?
文中的方法分為兩種:a. 修改參數; b. 使用 filter。涉及到的文件都在?theme?文件夾中(位于 /wp-content/theme/ 目錄)。每種方法只涉及到一個特定的文件。這樣就不需要修改 WordPress 核心文件了,那樣會很麻煩:每次升級 WordPress 都需要重新修改,而且容易引起錯誤。
前一種方法只涉及到?comments.php?文件。大多數主題都會有這個文件。雖然并不是一定要有 comments.php 文件,只不過這是 WordPress 推薦的方式。
第二種方法只需要修改 functions.php 文件。同樣的,大多數主題都會有這個文件。主題自定義的功能都在這個文件中。與上面不同的是,如果你的主題中沒有這個文件,你可以自己創建一個。
2. 評論框簡史?
在版本 3 之前,整個 WordPress 評論框的全部代碼都是在主題的 comments.php 文件中的。這使得修改它非常的方便。不過帶來的問題就是讓主題看起來有些亂。
開發人員也注意到了這個問題,然后從?WordPrss 3 開始,評論框就被精簡為一個函數了,直接在主題的相應位置調用此函數:
comment_form(); ?>
從此,基本上所有的主題都會直接使用此函數生成默認的評論框,頂多就是在 CSS 樣式定義上各有各的風格。一切變得極其簡潔。
然而問題也隨之而來:如果有人想要改變評論框,那就需要多動動腦子了。
3. 兩種方法?
有兩種方法可以對評論框進行自定義,能完成的任務也一樣。要采用哪一種方法一方面看你要干什么,另一方面就看個人的喜好了。
注意:方法 2 是我個人非常推薦的方法,會覆蓋掉方法 1。
方法 1: 在 COMMENTS.PHP 中更改?COMMENT_FORM() 調用?
這個方法用于以下場景是比較合適的:
- 修改評論框的所有細節
- 修改評論框中各字段(field)的標記,包括標簽(label)
- 其實,要完成想我開頭提到的那種修改目的,這個方法也是很好的
這個方法是通過給通常的評論框函數調用
comment_form(); ?>
增加一些細節來實現修改的。這些細節通過參數來傳遞給該函數。要改變什么細節,就指定相應的參數。例如:
comment_form( $args, $post_id ); >
其中,
- $args:可選,是一個數組(array()),包含用于評論框的字符串和字段等的配置內容,如果不寫該參數,就使用默認的(見下面);
- $post_id:可選,指定要在 ID 為 $post_id 的文章下產生該評論框,如果不寫該參數,就會使用當前文章的 ID(即在每篇文章下產生該文章的評論框)
以類似下面的形式定義上面的參數 $args 數組 array() :
$args = array(
'parameter_name' => 'value',
'another_parameter' => 'value'
));
默認的參數數組內容如下(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" />
' ) ) );
能夠使用的參數基本上包括了評論框的各個方面的細節。WordPress Codex entry on comment_form()?中對可用的參數有較為全面的介紹,并指明了參數的默認值(也就是不做修改時的樣子)。
省事的話,也可以不定義參數數組 $args = array(…,而是直接將要修改的某個參數放在數組里直接調用。如下面的例子。
例如,默認情況下,在評論框的下面有一行字提示用戶可以在評論時使用某些 HTML 代碼,存儲這些提示文字的參數是?comment_notes_after
。想修改為別的提示,比如指向網站上某個說明頁面的鏈接(如隱私政策頁面),就可以直接將原始函數調用改為:
comment_form(array(
'comment_notes_after' => '
請訪問我們的 隱私政策 頁面。
'
));
?>
再例如,可以通過下面的形式,將默認的評論框標題 Leave a Reply 改寫為別的,甚至可以是中文字符:
comment_form(array('title_reply'=>'雁過留聲,人過留名')); ?>
如果需要設置多個參數,需要在每個參數后面使用英文半角的逗號( , )隔開(最后一個參數后除外)。
如果要修改評論框的字段(?fields?)就稍微麻煩一些(此類應用建議使用下面的方法 2)。需要在上面參數數組 array 的里面創建一個字段數組(下面例子中將其命名為(賦值給) $fields ),然后通過 filter 來調用它。新數組 $fields 將被 ‘fields’ 參數調用。下面的例子中在 $fields 數組中指定了 author 字段的內容(包含了顯示它所需要的所有 HTML 代碼):
comment_form(array(
$fields = array(
'author' => '
' . '' . __( 'Your Name' ) . ' . $aria_req . ' />
'
);
));
?>
這樣就可以更加精細地控制評論框中“作者”(author)的顯示方式。上例中將默認的標簽 “Name” 改成了 “Your Name”,并將默認值(value)設置成了“Your First and Last Name”。現在只需要在參數數組 array 中調用該字段定義即可:
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 字段,所以如果你將上面的代碼應用到自己的主題的話,評論框顯示出來就只有“姓名”那個欄目了。當然,你也可以比照這個例子繼續編寫其它字段,email(郵件地址)和 url(網站地址)的代碼。
這里給個直接修改參數形式的,稍作修改的評論框三個字段的修改代碼,其中按照 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="個人主頁網址" size="30" value="'.$comment_author_url.'" name="url" id="url">p>'
)
)
);
方法 2: 在 FUNCTIONS.PHP 中使用 HOOK FILTER?
這種方法比較適合于
- 省略或改寫字段(姓名、郵件、網址)
- 當你想使用更加貼近核心也是最徹底的方法的時候
該方法采用 WordPress 的濾器(filters)來實現。使用 filter,可以在評論框最后輸出之前對其進行修改(所以會覆蓋掉在 comments.php 中的修改)。
這個就需要在主題的?functions.php?文件中操作了。將編寫的代碼放到 functions.php 文件的結尾即可(如果結尾有 ?>,則應放在 ?> 之前)。
如果你的主題沒有 functions.php 文件,自己創建一個就行了(簡單的 Windows 記事本程序就可以操作了),但是要注意的是,自己創建的時候應該先在該文檔中輸入下面的代碼做為開頭,
然后另起一行輸入自定義的代碼。先創建一個函數(function)來定義需要進行的操作。不過似乎只能用來操作(修改顯示方式等)字段(fields),比如三個輸入內容 author(姓名)、email(電郵)和 url(網址)。
例如,移除某字段:
add_filter('comment_form_default_fields', 'mytheme_remove_commentform_fields');
function mytheme_remove_commentform_fields($fields){
$fields['email'] = ''; // 后面的參數留空表示移除 email 字段
$fields['url'] = ''; // 移除 website 字段return $fields;
}
程序的名稱就隨意啦,只要保證上面一行 add_filter 中調用的與之一致就行了。
再比如修改 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;
}
需要注意的是,如果要修改某個字段,必須將該字段相關的所有部分 —— 標簽、輸入框等 —— 都定義在里面,否則……自己看看就知道了。
實際上也可以通過掛鉤 filter 來增加新的字段。只不過,如果沒有相應的 WordPress 數據庫操作,新加的字段并不會存儲到數據庫中,只能是個擺設。新加字段的方法是:
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 方式的,稍作修改的評論框三個字段的修改代碼,與前面參數傳遞方式那個實現的操作是一模一樣的,只不過這個要用在 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="個人主頁網址" 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 文件中,稍作修改即可。不過,如果要改變評論框出現的位置,那還是需要動一下 comments.php 文件的。
實際上,將下面的代碼保存下來,隨時可以往里面填寫東西來實現修改(每行前面的 // 表示注釋掉改行,即讓它不起作用,所以,要用的話就把 // 刪掉):
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篇文章:
- http://chipcullen.com/altering-the-comment-form-in-wordpress/
- http://www.1stwebdesigner.com/wordpress/comment-form-customization/
如果你有什么建議和疑問,請在下方留言。?
本文發表于水景一頁。永久鏈接:<https://cnzhx.net/blog/altering-wordpress-comment-form/>。轉載請保留此信息及相應鏈接。