WordPress comments form comes with predefined fields like Name, Email, Website and Comment. Here the comment field comes at the top of the form which one may not like. This is how it originally looks like.

What if you want to put the comment field after all other fields are loaded? Well, this is doable by using the filter hook provided by WordPress. Take a look at the following piece of code.

/**
 * Filter to move comment field to the last
 */
add_filter( 'comment_form_fields', 'wtitheme_comment_form_fields', 10, 1 );

/**
 * Move comment field to the last
 * @param $comment_fields
 * @return mixed
 */
function wtitheme_comment_form_fields($comment_fields) {
    if (isset($comment_fields['comment'])) {
        $comment_field = $comment_fields['comment'];
        unset($comment_fields['comment']);
        $comment_fields['comment'] = $comment_field;
    }

    return $comment_fields;
}

What we have done is

  • Attached a function to the hook.
  • Passed the available fields array to the function.
  • Checked if there is a comment field present and unset that from the available fields array. You can skip this though as there will be a comment field. However, this is helpful in case you target some other fields which may or may not be there.
  • Then appended the same comment field to the remaining available fields. Thus putting the comment field to the last after all other fields.

This is how it looks like after the hook is applied. Hope this helps you. Happy coding 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.