How to Automatically Add a Signature to Admin Comments in WordPress

Home » Snippets » How to Automatically Add a Signature to Admin Comments in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

As a website owner or administrator, you likely spend a lot of time leaving comments on your site. Maybe you’re responding to a reader’s question or providing feedback to a guest author. Whatever the reason, you may want to consider adding a signature to your admin comments. Having a signature not only adds a personalized touch to your site, but it can also help identify who is leaving a comment, especially if you have multiple admins. In this article, we’ll guide you through the process of automatically adding a signature to all of your admin comments in WordPress.

					function wpturbo_add_comment_signature_to_admin( $comment_text, $comment = null ) {
    if ( ! is_admin() ) {
        return $comment_text;
    }
    $signature = "nn--n" . __( 'Sent from my WordPress site', 'wpturbo' );
    return $comment_text . $signature;
}
add_filter( 'comment_text', 'wpturbo_add_comment_signature_to_admin', 10, 2 );
				

In this tutorial, we will see how it is possible to automatically add a signature to admin comments in WordPress. By doing this, we can ensure that every comment that the admin is publishing on the website has a professional touch.

To achieve this, we can use the comment_text filter hook, which allows us to modify the comment text before WordPress displays it on the admin dashboard.

We start by defining a new function called wpturbo_add_comment_signature_to_admin(), which takes two arguments, $comment_text and $comment. The first argument contains the comment text that WordPress is about to display, and the second argument contains the comment object itself.

Inside the function, we check if the current user is an admin. If not, then we simply return the original comment text, since we don’t want to add the signature for regular users.

If the user is an admin, then we define the signature that we want to add. In this case, we simply append the text -- Sent from my WordPress site to the end of the comment text. We start the signature with a newline character n, so that the signature appears on a new line.

After defining the signature, we concatenate it with the original comment text using the PHP concatenation operator ., and return the resulting string.

Finally, we need to hook our function into the comment_text filter using the add_filter() function. We specify the wpturbo_add_comment_signature_to_admin function as our callback function, and set the priority to 10 and the number of arguments to 2, since our function takes two arguments. This ensures that our function is called whenever WordPress applies the comment_text filter.

Register an account to save your snippets or go Pro to get more features.