Comment Form Hooks Visualized

Most themes (e.g. TwentyTen) use the comment_form() function to insert the comment form after posts. There are quite some hooks inside the function but they are hard to localize. The codex documentation isn't too helpful, neither.
To give you an easy overview the following diagrams visualize the points where the various hooks are anchored. The number of available hooks depend on the discussion settings and the user's capabilities.

In the most common scenario the user is not logged in, is allowed to comment on the article and the comments are not closed:

There are six hooks available:

  • comment_form_before
  • comment_form_top
  • comment_form_before_fields
  • comment_form_after_fields
  • comment_form
  • comment_form_after

You might have noticed that the hooks comment_form and comment_form_after seem to be anchored almost at the same point but depending on the user's role and discussion settings they are not always available so you should take care which hook you are using in your code.


User logged in
If you are logged in you have fewer hooks available since the name, email and URL input fields are not needed. The missing hooks are

  • form_comment_before_fields
  • form_comment_after fields


User is not logged and "Users must be registered and logged in to comment" activated
If your blog is configured that only registered users can comment, an unregistered user will see this comment form and additionally the hook comment_form_must_log_in_after is available. Please notice that in this case the hook comment_form is left out.


Comments closed
If the comments on the post are closed you have only one hook left (comment_form_closed) since the form is not displayed at all:

Comments are closed.

7 comments

  1. Rev. Voodoo

    Great post, it's one of those posts I wish I'd written. When I was first getting used to the comment system, I kep having to google for these until I had them memorized. It's nice to have them in one place, should be real helpful for folks.

  2. Randy

    This is a great way to visualize what hooks are available and when to use them. It would be incredible if you did this for other areas besides comments too!
    Great post! Thanks!

  3. Brandon

    I really how clear this post and diagram are. Have you done it for any other areas of WordPress? If not you should!

  4. Paweł Pela

    That's great, mate! WP Codex never explained this so clearly :)

  5. Patrick

    I haven't run across too many posts regarding the comment_form() function. This is a good one :) I find that people either a) coming from older WP versions where the fields and hp was in one file or b) less experienced coders are running into issues with the new way of doing things and tend to get a bit frustrated.

    This should help!

  6. gr4y

    Thanks for this nice overview. But styling these new comment form is a huge pain in the *beeep*

One pingback

  1. Weekly WordPress Review » WPCanada