强制登录用户在评论时提供更多数据

时间:2013-02-14 作者:hnnnng

我已经使用挂钩在我的评论中添加了更多数据。我添加了一组单选按钮,以便用户可以选择其中一个单选按钮。一切正常,但新添加的单选按钮在您登录后不会显示。。它们仅在您未登录时显示并工作。登录的用户只能看到注释框。

如何向登录用户显示新添加的字段以及注释框?

这就是我在函数中添加的内容。php

function add_comment_fields($fields) {

    if(is_user_logged_in()) {

        $fields[\'age\'] = \'<p class="comment-form-age"><label for="age">\' . __( \'Age\' ) . \'</label>\' .
        \'<input id="age" name="age" type="text" size="30"\' . $aria_req . \' /></p>\';
        return $fields;
    }
}
add_filter(\'comment_form_default_fields\',\'add_comment_fields\');


function add_comment_meta_values($comment_id) {

    if(isset($_POST[\'age\'])) {
        $age = wp_filter_nohtml_kses($_POST[\'age\']);
        add_comment_meta($comment_id, \'age\', $age, false);
    }

}
add_action (\'comment_post\', \'add_comment_meta_values\', 1);

1 个回复
SO网友:Simon

一个想法是将所有附加数据保存为user meta, 这将允许您在登录用户发布评论时检索它,并将其副本存储在评论旁边,就像对未登录用户一样。当然,这取决于数据的类型,以及每个注释必须具有唯一性的程度。

另一种方法可能是comment_form_top 操作,由调用comment_template() 打印评论表单之前。假设您已使用comment_form_default_fields 过滤器,然后执行如下操作:

function get_extra_fields() {
    return array(\'age\' => \'<p class="comment-form-age"><label for="age">\' . __( \'Age\' ) . \'</label>\' .
        \'<input id="age" name="age" type="text" size="30"\' . \'\' . \' /></p>\');
}

function add_comment_fields($fields) {

    $fields = array_merge($fields, get_extra_fields());

    return $fields;
}
add_filter(\'comment_form_default_fields\',\'add_comment_fields\');


function add_comment_meta_values($comment_id) {

    if(isset($_POST[\'age\'])) {
        $age = wp_filter_nohtml_kses($_POST[\'age\']);
        add_comment_meta($comment_id, \'age\', $age, false);
    }

}
add_action (\'comment_post\', \'add_comment_meta_values\', 1);


function add_comment_fields_logged_in() {
    if (!is_user_logged_in())
        return;

    $fields = get_extra_fields();

    foreach ( $fields as $name => $field ) {
        echo apply_filters( "comment_form_field_{$name}", $field ) . "\\n";
    }
}
add_filter(\'comment_form_logged_in_after\', \'add_comment_fields_logged_in\');
您可能还需要进行一些检查,以验证表单等提交了所有必需的数据,但这可能也适用于未登录的用户。

结束

相关推荐

Bones_Comments()和Comments.php有什么不同

我开始使用Bones HTML5 Wordpress入门主题,但与Bones中的一些内置功能有点混淆,有function bones_comments() 在函数中。php,但我在文件中找不到使用或调用此函数的任何地方。我看了一下函数中的代码,它看起来像是一些输出wordpress注释的代码,但也有注释。wordpress中comment\\u template()的php。所以真的搞不明白为什么bones添加了bones\\u comments()函数而没有使用它。bones\\u comments()