强制向登录用户显示备注表单中的所有字段

时间:2020-01-20 作者:fatihturan

我想强制向登录的用户显示所有字段(如果可能,这些字段可以预先填充用户自己的电子邮件和姓名数据)。所以我发现a reply 关于Wordpress开发/SE社区,但我不知道如何解决。如何做到这一点?

1 个回复
最合适的回答,由SO网友:Greg Winiarski 整理而成

您可以将下面的代码粘贴到主题函数中。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可能会强制使用保存在配置文件中的用户详细信息)。

相关推荐

Comments.php保留评论日期/时间,但删除日期/时间的#超级链接

我在谷歌上搜索了这个问题,似乎找不到解决方案。。。在评论中,我试图从评论日期/时间中删除超链接,当您将鼠标悬停在评论日期上方时,它会将超链接(示例/#comment-210)链接到以下评论。。。我可以在函数中输入什么。php删除链接,我想保留日期/时间文本。。