#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.

<?php
/**
* Filter the comment author name for administrators
*
* @see wp_filter_comment(), sanitize_comment_cookies()
*
* @param string $name The comment author name.
* @return string The filtered author name.
*/
function wpdocs_comment_author_name( $name ) {
if ( current_user_can( 'manage_options' ) )
return sprintf( __('Admin: %s', 'yourtextdomain'), $name );
else
return $name;
}
add_filter( 'pre_comment_author_name', 'wpdocs_comment_author_name' );

What is this?

In the scheme of things, hooks and filters are some of the least documented parts of the WordPress code base. A few years ago, the WordPress docs team developed a roadmap for the future of WordPress documentation and as I was part of the team that developed it, I’m none too aware of where the dry patches are. So I decided to do this thing.

From Adam Brown’s hooks list, I pulled 2500+ filter and action hooks. That’s a lot of examples to write but maybe I can help some people discover new and interesting ways to extend WordPress.

These examples may not all be awesome, but that’s the beauty of crowd-sourced code reviews, we’ll get it as right as we can. And of course the bonus of all this, is that hopefully we can put some/many/all of these examples to use in the Code Reference, wherever filters may fall into the new roadmap.