• 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 15 replies - 1 through 15 (of 18 total)
  • Moderator bcworkz

    (@bcworkz)

    $user_identity is undefined in your callback. It’s apparently defined on comments.php somewhere. Use get_currentuserinfo() to get the value you need.

    Thread Starter theredeclipse

    (@theredeclipse)

    Can you enlighten me how to do it? Tried few variations but it still returns an empty form.

    And tell me please is it worth to move all hooks for a template to functions.php? Or instead of that better to store in a needed file, like comments.php. As I know, functions.php better to use only for needs of selected theme, but what about my_theme_comment and comment_form? It just need to load on a selected page, but functions.php loads with every theme page. Or I am wrong?

    Moderator bcworkz

    (@bcworkz)

    Oops, my bad. Should be wp_get_current_user(). Same idea though.

    $current_user = wp_get_current_user();
    $user_identity = $current_user->user_login;

    I recommend placing all functions on functions.php or any included/required descendant of this file. Yes it is very slightly less efficient, but it greatly improves maintainability and readability keeping everything in a logical location. The one exception would be adding a hook on a template to prevent the callback from being applied to broadly. Simpler than trying to determine if a template is loaded or not within the callback.

    Exception to the exception is if the condition is easily determined within the callback, like where the query object is passed to the callback and we merely need to check the query to see which template is going to be used.

    The efficiency gained by scattering functions on various template files is so minimal it’s not worth considering. Think about all the files and code loaded to load the WP environment. An extra dozen lines or two is not going to matter much!

    Thread Starter theredeclipse

    (@theredeclipse)

    thanks for the reply. But it doesn’t work, guess I doing something wrong, for my shame I pretty lame with PHP(but its on what I working on) :]

    Can you apply it into my code, I’ll be very appreciated

            '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>',
    Moderator bcworkz

    (@bcworkz)

    That code is for users not logged in. There’s no way to know a user name if they are not logged in.

    To affect the line “You logged as [user login]. Log out of this account.”, place my code just above the // Form Options Array line but below the closing ); from the statement above.

    Thread Starter theredeclipse

    (@theredeclipse)

    Oh, I see, I’ve tried to apply this under code with result as blank page. Now tried as you said, as least site loads but “leave a comment” field is not.

    Moderator bcworkz

    (@bcworkz)

    Well, making progress at least. Sorry for the broken code, it’s sometimes difficult to grasp the full context when helping others. I think we just need a check that the user is logged in before attempting to get the user object. Also, for good measure, ensure something is assigned to $user_identity even if it’s not used for output, since it’s always used to construct the argument.

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

    Still untested though. Is this code is being applied to the default comments form? If so, and if this doesn’t help, there’s something else going on. I’ll then take some extra time to do my own testing. I don’t have access to my test rig right now.

    Thread Starter theredeclipse

    (@theredeclipse)

    Unfortunately result is same, comment form doesn’t load.

    Btw, thanks for clarifying regrading usage of functions.php, though I’ve assumed same – just logically a better place for a bunch of code.

    If it may help I can give access to my site, its still in develop so its offline, would do no harm.

    Moderator bcworkz

    (@bcworkz)

    Yeah, I’m going blind I think. There’s several more variables that are undefined that are causing the problems. The following placed at the very beginning resolves them, but I don’t know if the values I’ve assigned are valid.

    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';

    At least the “You logged in as [user login]” works.

    Thread Starter theredeclipse

    (@theredeclipse)

    code should be applied in same spot? I’ve tired this, but it still returns empty reply form :\

    Moderator bcworkz

    (@bcworkz)

    No, the very first code to execute when the function is called. Above // Fields Array

    Thread Starter theredeclipse

    (@theredeclipse)

    Oh, I see. Well, form is loading now but still no username. Btw, I have installed bbpress and buddypress, not sure if these modules might be a case just let you know.

    Moderator bcworkz

    (@bcworkz)

    Hmmm. It’s odd that the user login would not be available, but plugins can do almost anything. I did test your code on a simple, default installation and it works fine with that last bit of adjustments. You could try temporarily deactivating each plugin in turn. When the name appears, you’ve found the plugin responsible. You could then post a question in the dedicated support forum of that plugin. Also try swapping to the twentysixteen theme for a test, themes have been known to do odd things too.

    Thread Starter theredeclipse

    (@theredeclipse)

    I’ve tried to test it on almost fresh WP installation(just a few plugins were installed but all of them deactivated). With my theme installed it still doesn’t work aswell copied code of comment form into twentyfifteen theme with same result. Maybe I’ve missed something, can you check this hook again with your applied 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>',
    
        );
    	
    	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';
    
        // 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( __( 'Вы вошли как  <a href="%1$s">%2$s</a>. <a href="%3$s" title="Log out of this account">Выйти?</a>' ),
                admin_url('profile.php'),  $current_user, 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;
    }
    Thread Starter theredeclipse

    (@theredeclipse)

    Just noticed I’ve enabled your part of code just above $form_options, but i have been tried above $fields aswell

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