在開發(fā)留言表單的過程中,客戶需要提交的字段都是不同的,而WordPress默認(rèn)的只有四個(gè)字段:郵箱、網(wǎng)址、姓名和內(nèi)容。如果我們想要增加幾個(gè)字段就需要使用wp_insert_comment鉤子。
wp_insert_comment鉤子主要給評(píng)論表單增加新的字段。
語法
do_action( 'wp_insert_comment', int $id, WP_Comment $comment )
實(shí)例
add_action('wp_insert_comment','wp_insert_tel',10,1);
function wp_insert_tel($comment_ID) {
$tel = isset($_POST['tel']) ? $_POST['tel'] : false;
$qq = isset($_POST['qq']) ? $_POST['qq'] : false;
update_comment_meta($comment_ID,'tel',$tel);
update_comment_meta($comment_ID,'qq',$qq);
}
使用這個(gè)鉤子需要請了解update_comment_meta()函數(shù)
通過這種方法已經(jīng)可以在數(shù)據(jù)表中添加新的的鍵值,但是如果想要在我們的后臺(tái)評(píng)論板塊中顯示出來還需要進(jìn)行下面的操作。
實(shí)例
add_filter( 'manage_edit-comments_columns', 'my_comments_columns' );
function my_comments_columns( $columns ){
$columns[ 'tel' ] = __( '電話' );
$columns[ 'qq' ] = __( 'QQ號(hào)' );
return $columns;
}
add_action( 'manage_comments_custom_column', 'output_my_comments_columns', 10, 2 );
function output_my_comments_columns( $column_name, $comment_id ){
switch( $column_name ) {
case "tel" :
echo get_comment_meta( $comment_id, 'tel', true );
break;
case "qq" :
echo get_comment_meta( $comment_id, 'qq', true );
break;
}
}
manage_edit-comments_columns鉤子可以用來更改評(píng)論模塊顯示字段的值,比如我們提交了一個(gè)名為“tel”的鍵,但我們的網(wǎng)站是針對國內(nèi)客戶,需要把“tel”換成“電話”,這時(shí)就要使用這個(gè)鉤子了。
manage_comments_custom_column鉤子可以用來在評(píng)論模塊添加自定義的字段
get_comment_meta()函數(shù)用來輸出值,詳細(xì)了解此函數(shù)請點(diǎn)擊查看get_comment_meta()函數(shù)