Select Page

Do you want to automatically add a signature to all comments created by WordPress administrators? It’s really easy.

Place this code in your theme’s functions.php file or create a custom plugin with this code:

/**
 * Custom function to automatically add a signature to admin comments
 *
 * @param array $commentdata An array of comment data
 * @return array $commentdata The modified array of comment data
 */
function add_signature_to_admin_comments( $commentdata ) {
  // Check if the current user is an admin
  if ( current_user_can( 'manage_options' ) ) {
    // Get the current user's display name
    $current_user = wp_get_current_user();
    $display_name = $current_user->display_name;

    // Append the signature to the comment text
    $commentdata['comment_content'] .= "\n\n--\n$display_name";
  }

  return $commentdata;
}
add_filter( 'preprocess_comment', 'add_signature_to_admin_comments', 10, 1 );

The function takes one argument: $commentdata, an array of comment data.

Inside the function, the ‘current_user_can’ function is used to check if the current user is an admin. If the current user is an admin, the ‘wp_get_current_user’ function is used to get the current user’s display name, and the signature is appended to the comment text using the ‘$commentdata’ array.

The ‘$commentdata’ array has a key called ‘comment_content’, which holds the comment text. The signature is appended to the comment text using the ‘.’ operator, which concatenates two strings. The signature consists of the current user’s display name, preceded by two newline characters and two hyphens.

Finally, the function is hooked to the ‘preprocess_comment’ filter using the ‘add_filter’ function, which applies the changes to the comment data before storing it in the database.

This code will automatically add a signature to admin comments in WordPress. You can modify the code to customize the signature or add it to comments made by users other than admins as desired.