Using filter to add additional fields to comment_form()

There are a couple other hooks in the comment form that you can use. Where you’re hooking on only displays if the user isn’t logged in. If you want that field for all users (logged in or not), you need to add your form by hooking into both comment_form_after_fields and comment_form_logged_in_after, both of which are actions, and echo out the new field.

<?php
add_action( 'comment_form_logged_in_after', 'pmg_comment_tut_fields' );
add_action( 'comment_form_after_fields', 'pmg_comment_tut_fields' );
function pmg_comment_tut_fields()
{
    ?>
    <p class="comment-form-title">
        <label for="pmg_comment_title"><?php _e( 'Title' ); ?></label>
        <input type="text" name="pmg_comment_title" id="pmg_comment_title" />
    </p>
    <?php
}

Check out this tutorial I wrote (the example above is from it). Covers everything from adding fields to saving the data to adding a meta box so you can edit the extra fields on the back end too.

Leave a Comment