#1: pre_comment_author_name

The pre_comment_author_name filter allows you to modify a comment author’s name before the comment is created or updated.

It’s evaluated in:

The value of pre_comment_author_name is also filtered through:

Example:

Let’s say you want to prefix commenters’ names with the word ‘Admin’ if they are administrators on your site. The following example will turn ‘Some Name’ into ‘Admin: Some Name’.

We’ll use the manage_options capability to determine if the commenter is an admin, and if so, filter the name. If they don’t have the capability (aren’t an admin), we’ll return the name unfiltered.

Have a suggested improvement? You can fork the Gist and comment back with the link. If all is agreeable, I’ll merge in your changes. Crowd-sourced documentation FTW!

10 thoughts on “#1: pre_comment_author_name

  1. I feel like we could literally check the user role slug here since we’re displaying something indicating someone’s role, not what capability they have.

    Perhaps embedded gists would allow for easier collaboration on code examples though? On the other hand, owning data and all…

  2. New blog series: Filters of the Day | WerdsWords

  3. Week 1 Recap: Filters 1-12 – Filters of the Day

Leave a Reply

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