您可以将下面的代码粘贴到主题函数中。php文件,它应该为登录和注销的用户生成几乎相同的注释表单
add_filter( "comment_form_fields", function( $comment_fields ) {
if( is_user_logged_in() ) {
// Get an array of field names, excluding the textarea
$comment_field_keys = array_diff( array_keys( $comment_fields ), array( \'comment\' ) );
// Get the first and the last field name, excluding the textarea
$first_field = reset( $comment_field_keys );
$last_field = end( $comment_field_keys );
foreach ( $comment_fields as $name => $field ) {
if ( \'comment\' === $name ) {
echo apply_filters( \'comment_form_field_comment\', $field );
//echo $args[\'comment_notes_after\'];
} else {
if ( $first_field === $name ) {
do_action( \'comment_form_before_fields\' );
}
echo apply_filters( "comment_form_field_{$name}", $field ) . "\\n";
if ( $last_field === $name ) {
do_action( \'comment_form_after_fields\' );
}
}
}
return array();
}
return $comment_fields;
}, 2000 );
请注意,我没有检查在保存评论时是否会使用表单中输入的数据,您应该检查它(因为WordPress可能会强制使用保存在配置文件中的用户详细信息)。