Comment form customization

Is there an 'easy' way to customize 'Comments form'? Any example will be highly appreciated

What I need is to change the text displayed like for example Email, Comment, Logged in....etc, after searching the web, I can see that it was an easy task back in WordPress 2.9 or less, but it looks like a pretty complex task in WP 3.0+

Thanks in advance for your time and efforts

Best Regards

    bpilot

    Hi masonjames,

    comments.php in twentyten theme include only one call to comment template:


    <?php comment_form(); ?>

    Now if I need for example to translate the labels next to each field or even change them, like for example change (Name) to (First Name).. (This is just a hypothetical example), it is very complex procedure which is hardly documents on the web

    I've tried to follow couple of tutorials, but didn't reach anything as they were either incomplete or examples offered there are wrongly coded

    Regarding page.php, that's a different story as the function:
    <?php comments_template( '', true ); ?>

    is used to load the comment template under wp-includes/comment-template.php which shows the old WP comments.php file content but unfortunately the file have a big warning on the top saying:

    This file is here for Backwards compatibility with old themes and will be removed in a future version

    So I was wondering is there anyway I can customize the comment form template without going through any 'compatibility problems' in the future?

    Thanks in advance for your time

    bpilot

    Thanks a lot Ulrich, both articles did include a lot of useful information, yet I am still confused about two major points here:

    A) Anyway, after extensive research I found someone whom was trying to add more fields to the form and his code was as follows:


    add_filter('comment_form_default_fields','my_comment');
    function my_comment($fields) {
    // New fields
    $fields['date'] = '<label>Date:</label><input type="text" name="date" value="'.(!empty($_POST['date']) ? $_POST['date'] : '').'" />';
    $fields['start_time'] = '<label>Start time:</label><input type="text" name="start_time" value="'.(!empty($_POST['start_date']) ? $_POST['start_date'] : '').'" />';
    $fields['end_time'] = '<label>End time:</label><input type="text" name="end_time" value="'.(!empty($_POST['start_date']) ? $_POST['start_date'] : '').'" />';
    $fields['early_birds'] = '<label>No early birds?:</label><input type="checkbox" name="early_birds" '.(isset($_POST['early_birds']) ? 'checked' : '').' />';
    $fields['bad_weather'] = '<label>Bad weather?:</label><input type="checkbox" name="bad_weather" '.(isset($_POST['bad_weather']) ? 'checked' : '').' />';
    // Remove unwanted default fields
    unset($fields['url']);
    return $fields;
    }

    After testing the code it added some fields to the form. Even though this is not my main issue but I wonder how will the data in those fields get recorded in the database? I mean how will those new fields get added to the comments table so that they appear when displaying any post comments?

    B) Now regarding modifying the original fields (which is my main issue and problem here), based on very limited some code examples I found on the web I saw something like:


    '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>',
    'id_form' => 'commentform',
    'id_submit' => 'submit',
    'title_reply' => __( 'Leave a Reply' ),
    'title_reply_to' => __( 'Leave a Reply to %s' ),

    Where is the reference for all default items in the comment form? For example where is the reference that shows that:
    'title_reply' is __( 'Leave a Reply' )
    or
    'author' is '<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>',

    ?

    As I couldn't find such reference on the web

    Thanks in advance for your time and efforts

    Ulrich

    First off, in case you are not aware, there is a plugin for Extra Comment Fields. The version compatible with WP3 can be downloaded here: http://www.goashleygo.com/extra-comment-fields.zip

    Now on your questions:

    Even though this is not my main issue but I wonder how will the data in those fields get recorded in the database?

    Example to save the date and start_date fields (do the same for the other fields):
    add_action ( 'comment_post', 'save_comment_fields' );
    function save_comment_fields( $comment_id ) {
    add_comment_meta( $comment_id, 'date', $_POST['date'], true );
    add_comment_meta( $comment_id, 'start_date', $_POST['start_date'], true );
    }

    To display the saved data use get_comment_meta in the callback function. The callback function is the function which sets the layout of the comments list. See 'twentyten_comment' in Twenty Ten theme, e.g.
    echo 'Date: ' . get_comment_meta( get_comment_ID(), "date", true );

    Note: The data is saved in the table '*_commentmeta' in the database.

    Where is the reference for all default items in the comment form?

    See comment_form in the WordPress codex.

    bpilot

    Hi Ulrich,

    Thanks for the default items reference, that helped me a lot here...OK I've managed to write the following code which changes text in default fields as we wanted but the form is shown twice and some fields are displayed without a reason, like the title_reply_to for example...so can you please check and tell me what I might be doing wrong here?

    $ourcustomfields = array(
    'author' => '<p class="comment-form-author">' . '<label for="author">' . __( 'First, Last 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">' . __( 'Your 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>',

    );

    comment_form(array('fields' => $ourcustomfields));

    Thanks in advance for your time and efforts

    Ulrich

    Sorry, I missed your reply.

    If you want just to modify to author and email fields labels, I think it would be better to use the 'gettext' filter instead:

    function filter_gettext( $translation, $text, $domain ) {
    $translations = &get_translations_for_domain( $domain );

    if( !is_admin() ) {
    if ( $text == 'Name' ) {
    return $translations->translate( __( 'First, Last Name', 'textdomain' ) );
    } elseif ( $text == 'Email' ) {
    return $translations->translate( __( 'Your Email', 'textdomain' ) );
    }
    }

    return $translation;
    }
    add_filter( 'gettext', 'filter_gettext', 10, 3);

    bpilot

    Hi Ulrich,

    Thanks a lot for your reply, this solved the problem instantly...highly appreciated

    If you don't mind can you please give me a hint where I can find reference to this line:


    $translations = &get_translations_for_domain( $domain );

    I tried searching WordPress functions for (get_translations_for_domain) function but couldn't find anything, so is a core PHP function?