$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>comments - custom comment_form fields not displaying|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

comments - custom comment_form fields not displaying

matteradmin6PV0评论

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?

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?

Share Improve this question edited Oct 15, 2012 at 2:18 user1255049 asked Oct 15, 2012 at 2:05 user1255049user1255049 2413 gold badges14 silver badges37 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

Post a comment

comment list (0)

  1. No comments so far