COMMENT_FOR()用户登录时自定义字段不可见

时间:2017-05-06 作者:Frederick M. Rogers

我添加了一些自定义表单字段using comment_for() 以及过滤comment_form_default_fieldscomment_form_defaults 钩子用于使用自定义注释字段以及作者、电子邮件和url。修改工作与用户注销时一样好,但如果登录自定义表单字段,则会消失,只留下提交按钮。我的修改/筛选是否有任何原因导致这种行为?

function.php

/** 
 * CUSTOM COMMENT FORM
 * @method magneton_comment_form
 * Replaces default markup for the WordPress comment form.
 */
function magneton_comment_form_fields( $fields ) {
    //$commenter      = wp_get_current_commenter();
    //$user           = wp_get_current_user();
    //$user_identity  = $user->exists() ? $user->display_name : \'\';
    $req            = get_option( \'require_name_email\' );
    $aria_req       = ( $req ? \' aria-required="true"\' : \'\' );

    $fields = array(

        \'author\'        => \'<div class="form-group form-inline">
                            <label for="comment-author" class="sr-only">\' . __( \'Author\', \'magneton\' ) . \'</label>
                            <input type="text" name="author" id="comment-author" class="form-control author-field" placeholder="\' . __( \'Author (required)\', \'magneton\' ) . \'"\' . $aria_req . \'>\',
        \'email\'         => \'<label for="comment-author-email" class="sr-only">\' . __( \'E-Mail\', \'magneton\' ) . \'</label>
                            <input type="email" name="email" id="comment-author-email" class="form-control email-field" placeholder="\' . __( \'E-Mail (required)\', \'magneton\' ) . \'"\' . $aria_req . \'>\',
        \'url\'           => \'<label for="comment-author-url" class="sr-only">\' . __( \'Website\', \'magneton\' ) . \'</label>
                            <input type="url" name="url" id="comment-author-url" class="form-control url-field" placeholder="\' . __( \'Website\', \'magneton\' ) . \'">
                            </div>\',
        \'comment_field\' => \'<textarea name="comment" id="comment" class="form-control comment-field"  aria-required="true" rows="10"></textarea>\'
    );

    return $fields;    

}
add_filter( \'comment_form_default_fields\', \'magneton_comment_form_fields\' );

/** 
 * REPLACE ORIGINAL COMMENT FORM
 * @method magneton_comment_form
 * Replaces default markup for the WordPress comment form.
 */
function magneton_comment_form_defaults( $defaults ) {

    if ( isset( $defaults[ \'comment_field\' ] ) ) {
        $defaults[ \'comment_field\' ] = \'\';
    }

    return $defaults;

}
add_filter( \'comment_form_defaults\', \'magneton_comment_form_defaults\' ); 

comment.php

$comments_args = array(                
                \'id_form\'       => \'comment-form\',
                \'class_form\'    => \'comment-form\',
                \'class_submit\'  => \'btn btn-default\',
                \'title_reply\' => __( \'Leave a reply\', \'magneton\' ),                
                \'label_submit\' => __( \'Submit Comment\', \'magneton\' )
            );
LINK: DEMO

1 个回复
SO网友:Frederick M. Rogers

好的,所以我尝试了这一点,结果正如预期的那样,我用下面的代码替换了上面的代码(在comment.php中使用)。

要更正此问题,我只需检查用户是否使用is_user_logged_in() 然后将注释字段添加到$comments_args.

<?php $comments_args = array(                
            \'id_form\'       => \'comment-form\',
            \'class_form\'    => \'comment-form\',
            \'class_submit\'  => \'btn btn-default\',
            \'title_reply\' => __( \'Leave a reply\', \'magneton\' ),                
            \'label_submit\' => __( \'Submit Comment\', \'magneton\' )
        ); ?>

        <?php if ( !is_user_logged_in() ) : ?>

            <?php comment_form( $comments_args ); ?>         

        <?php else : ?>

            <?php $comments_args[ \'comment_field\' ] = \'<textarea name="comment" id="comment" class="form-control comment-field"  aria-required="true" rows="10"></textarea>\'; ?>

            <?php comment_form( $comments_args ); ?> 

        <?php endif ?>

Explanation

我最初对注释表单的自定义(如function.php文件中所示)添加了一个自定义<textarea> 默认值$fields 数组并筛选comment_form_default_fields 钩这导致用户登录时,注释字段以及其他自定义字段(用户/电子邮件/url)消失。我怀疑这是WordPress中的自然行为,因为这些信息是从用户配置文件中检索的。

欢迎对本解决方案的质量提出任何意见。

结束

相关推荐

Apply_Filters(‘the_content’)-是否使其忽略快捷代码?

我正在使用apply_filters(\'the_content) 因此,我可以在后端的wp编辑器中看到格式正确的内容。但是,这也会呈现内容中的短代码。我希望它忽略短代码,对其余内容进行过滤,基本上与发帖时一样。如果您在后端查看帖子内容,您将看到短代码,但如果您在网站的页面内查看它,您将看到呈现的短代码(其结果)。这可能吗?