0

My ideal comment form will have 3 fields- Name, Location, Comment. I'm using the following bits of code-

comments.php

<?php comment_form(
    array(
        'fields' => apply_filters( 'comment_form_default_fields', $fields ),
        'comment_notes_after' => ' ',
        'title_reply' => 'Please feel free to share your home owning hopes, dreams, or concerns?',
        'logged_in_as' => '',
    )
); ?>

functions.php

<?php

function my_fields($fields) {
$fields['Name'] = '<p>Name</p>';
$fields['Location'] = '<p>Location</p>';
return $fields;
}
add_filter('comment_form_default_fields','my_fields');

?>

As you can see on the live site, that's not working. Any ideas why/how to remedy?

1

I think you're a bit confused about how comment_form works. So let's take a look (this is in wp-includes/comment-template.php):

<?php
function comment_form( $args = array(), $post_id = null ) {
    // snip snip 
    $fields =  array(
        'author' => '<p class="comment-form-author">' . '<label for="author">' . __( 'Name' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) .
                    '<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>',
        'email'  => '<p class="comment-form-email"><label for="email">' . __( 'Email' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) .
                    '<input id="email" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /></p>',
        'url'    => '<p class="comment-form-url"><label for="url">' . __( 'Website' ) . '</label>' .
                    '<input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" /></p>',
    );

    $required_text = sprintf( ' ' . __('Required fields are marked %s'), '<span class="required">*</span>' );
    $defaults = array(
        'fields'               => apply_filters( 'comment_form_default_fields', $fields ),
        'comment_field'        => '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) . '</label><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>',
        'must_log_in'          => '<p class="must-log-in">' . 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'         => '<p class="logged-in-as">' . sprintf( __( 'Logged in as <a href="%1$s">%2$s</a>. <a href="%3$s" title="Log out of this account">Log out?</a>' ), admin_url( 'profile.php' ), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>',
        'comment_notes_before' => '<p class="comment-notes">' . __( 'Your email address will not be published.' ) . ( $req ? $required_text : '' ) . '</p>',
        'comment_notes_after'  => '<p class="form-allowed-tags">' . sprintf( __( 'You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: %s' ), ' <code>' . allowed_tags() . '</code>' ) . '</p>',
        'id_form'              => 'commentform',
        'id_submit'            => 'submit',
        'title_reply'          => __( 'Leave a Reply' ),
        'title_reply_to'       => __( 'Leave a Reply to %s' ),
        'cancel_reply_link'    => __( 'Cancel reply' ),
        'label_submit'         => __( 'Post Comment' ),
    );

    $args = wp_parse_args( $args, apply_filters( 'comment_form_defaults', $defaults ) );

        // $args get used to fill out the fields here
}

You need only to override the arguments you want to change. If you want custom fields, simply pass in the fields -- no need to bother with hook into comments_form_default_fields.

Overly simplified example:

<?php comment_form(array(
    'fields'    => array(
        'author' => '<input type="text" name="author" />',
        'location' => '<input type="text" name="location" />',
    ),
));

WordPress will handle saving the author field, but you'll have some more work to do for getting location saved. I wrote a tutorial about this.

Basically: hook into comment_post. Check $_POST for the field, save it:

<?php
add_action( 'comment_post', 'wpse69222_insert_comment', 10, 1 );
function wpse69222_insert_comment( $comment_id )
{
    // prolly should do more validation here?
    if( isset( $_POST['location'] ) )
        update_comment_meta( $comment_id, 'location', esc_attr( $_POST['location'] ) );
}

You might also want admin area fields and a way to edit that location from the admin, but that's another question.

  • thank you for you input(and the link to your tutorial). i've made the changes per your comment, and not only are the custom fields still not showing, but I'm now receiving the following error from wp_list_comments Warning: call_user_func() expects parameter 1 to be a valid callback, function 'my_custom_comments' not found or invalid function name in /home/content/56/9896856/html/wp-includes/comment-template.php on line 1336 – user1255049 Oct 15 '12 at 3:53
  • That's a separate issue. You can pass a callback to display the comment in wp_list_comments. It means the function my_custom_comments doesn't exist. So you can create it or remove the callback. Here's the custom callback in TwentyEleven for an example. – chrisguitarguy Oct 15 '12 at 5:08
  • I think the parent had issue where the fields are not shown when you are logged in. This is intended behaviour, and instead one must print the fields in two actions: comment_form_logged_in_after and comment_form_before_fields if it's required to show the fields in logged in as well as logged out users. – Ciantic Jan 27 '16 at 19:20

protected by Community Nov 21 '18 at 5:54

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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