• Hi,

    I currently developing a theme for WP and trying to figure out why comment_form doesn’t work as should when hook is stored in functions.php

    Here is the code

     add_filter('comment_form_defaults', 'rapid_comment_form');
    
    function rapid_comment_form($form_options)
    {
    
        // Fields Array
        $fields = array(
    
            'author' =>
            '<p>' .
            ( $req ? '<span class="required">*</span>' : '' ) .
            '<input class="block_field_input content_field_input" id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' placeholder="' . __( 'Name' ) . '" />' .
            '</p>',
    
            'email' =>
            '<p>' .
            ( $req ? '<span class="required">*</span>' : '' ) .
            '<input class="block_field_input content_field_input" id="email" name="email" type="text" value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' placeholder="' . __( 'Email' ) . '" />' .
            '</p>',
    
            'url' =>
            '<p>'  .
            '<input class="block_field_input content_field_input" name="url" size="30" id="url" value="' . esc_attr( $commenter['comment_author_url'] ) . '" type="text" placeholder="' . __( 'Website' ) . '" />' .
            '</p>',
    
        );
    
        // Form Options Array
        $form_options = array(
            // Include Fields Array
            'fields' => apply_filters( 'comment_form_default_fields', $fields ),
    
            // Template Options
            'comment_field' =>
            '<textarea id="comment" name="comment" class="block_field_input content_field_input_reply" placeholder="' . _x( '', 'noun' ) . '"></textarea>' ,
    		
    
            'must_log_in' =>
            '<p class="font_2">' .
            sprintf( __( 'You must be <a href="%s">logged in</a> to post a comment.' ),
                wp_login_url( apply_filters( 'the_permalink', get_permalink($post_id) ) ) ) .
            '</p>',
    
            'logged_in_as' =>
            '<div class="font_2">' .
            sprintf( __( 'You logged as  <a href="%1$s">%2$s</a>. <a href="%3$s" title="Log out of this account"></a>' ),
                admin_url('profile.php'), $user_identity, wp_logout_url( apply_filters('the_permalink', get_permalink($post_id)) ) ) .
            '</div></div></div>',
    
            'comment_notes_before' =>
            '<div class="font_2">' .
            __( '' ) . ( $req ? $required_text : '' ) .
            '</div></div></div>',
    
            'comment_notes_after' => '',
    
            // Rest of Options
            'id_form' => 'form-comment',
            'id_submit' => 'submit',
            'title_reply' => __( '<div class="font_1"></div>' ),
            'title_reply_to' => __( '<div class="font_1"> %s</div>' ),
            'cancel_reply_link' => __( '<div class="font_2"></div>' ),
            'label_submit'         => __( 'Post Comment' ),
    		'title_reply_before' => __( '<div class="block_line"><div class="content_header_align">' ),
    		'title_reply_after' => __( '' ),
    		
        );
    
        return $form_options;
    }

    So problem with this line

    
            'logged_in_as' =>
            '<div class="font_2">' .
            sprintf( __( 'You logged as  <a href="%1$s">%2$s</a>. <a href="%3$s" title="Log out of this account"></a>' ),
                admin_url('profile.php'), $user_identity, wp_logout_url( apply_filters('the_permalink', get_permalink($post_id)) ) ) .
            '</div></div></div>',

    It cannot display a username and as result shows “You logged as “. When I apply this code in comments.php everything works as should and displays username. Why?

Viewing 3 replies - 16 through 18 (of 18 total)
  • Moderator bcworkz

    (@bcworkz)

    There needed to be a few adjustments. Move this block to the very top:

    global $post;
    $req = true;
    $aria_req = '';
    $commenter = array();
    $commenter['comment_author'] = '';
    $commenter['comment_author_email'] = '';
    $commenter['comment_author_url'] = '';
    $post_id = $post->ID;
    $required_text = ' Required';

    You’ll probably want to translate ‘ Required’ in the last line above. It’s used when the user is not logged in. Leave the space in front.

    I also had to revise and insert my user name code. You can insert it in either location, it doesn’t matter. This version works:

    $current_user = 'anonymous'; //should never display as this
    if ( is_user_logged_in() ) {
       $user = wp_get_current_user();
       $current_user = $user->user_login;
    }
    Thread Starter theredeclipse

    (@theredeclipse)

    Holy moly, it works now! Thanks a lot!

    Moderator bcworkz

    (@bcworkz)

    Awesome! You’re welcome ??

Viewing 3 replies - 16 through 18 (of 18 total)
  • The topic ‘comment_from in functions.php’ is closed to new replies.