0

i have added some custom form fields using comment_for() as well as filtering the comment_form_default_fields and comment_form_defaults hook for the purposes of using a custom comment field as well as author, email and url. The modification work just fine as lone as the user is logged out, but if logged in the custom form fields disappear leaving only the submit button. Is there any reason my modifications/filters should cause this behavior ?

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

Ok, so i tried this and it worked as expected, i replaced the above code (used in comment.php) with the one bellow.

To correct this issue i simply checked if the user is logged in using is_user_logged_in() and then added the comment field to the $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

My original customization for of the comment form (as seen about in the function.php file) added a custom <textarea> to the default $fields array and filtered the comment_form_default_fields hook. The result of this caused the comment field as well as the other custom fields (user/email/url) to disappear when the user is logged in. I suspect this a natural behavior in WordPress since this information is retrieved from the users profile.

Appreciate any comments in regard to the quality of this solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.