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:
sanitize_text_field
- and
wp_filter_kses
- and
_wp_specialchars
on priority 30 in wp-includes/default-filters.php
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.
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…
Feel free to fork the gist and post back with the URL if you have a suggested improvement.
It would probably be better to do a sprintf() for translations, since word order might be different. So, sprintf( __( ‘Admin: %s ‘, ‘yourtextdomain’ ), $name ).
Feel free to fork the gist and post back with the URL if you have a suggested improvement.
Here you go: https://gist.github.com/jjeaton/6039101
Thanks, merged.
New blog series: Filters of the Day | WerdsWords
Sweet project.
This one worked great, but it’s missing a closing bracket on line 11.
Forked and added here: https://gist.github.com/kmarsden/6026886
Merged in your changes. Good catch, thanks!
Week 1 Recap: Filters 1-12 – Filters of the Day