Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use comment_form_logged_in filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
August 18, 2022
5 minutes read

comment_form_logged_in filter

Filters the ‘logged in’ message for the comment form for display.

To use the comment_form_logged_in filter, first you have to register it using add_filter. You can write this code into functions.php of your activated theme or in a custom WordPress plugin.

We at WePlugins always prefer to create a custom WordPress Plugin while using hooks so nothing breaks when you update your WordPress Theme in the future.

In the below live example, we have defined a function weplugins_modify_comment_form_logged_in_defaults which takes 3 parameters and we registered using add_filter. The first parameter comment_form_logged_in is the name of the hook, the second parameter weplugins_modify_comment_form_logged_in_defaults is the name of the function which needs to be called, the third parameter is the priority of calling the hook if the same hook is used multiple times and the last parameter is the number of arguments (if any) to be passed in the registered function.

Sometimes, you have to remove a registered hook so you can use remove_filter to remove the comment_form_logged_in filter.

Parameters

Below are the 3 parameters required to use this hook:

  • $args_logged_in: (string) The logged-in-as HTML-formatted message.
  • $commenter: (array) An array containing the comment author’s username, email, and URL.
  • $user_identity: (string) If the commenter is a registered user, the display name, blank otherwise.

Live Example

Below is an example of how you can use this hook.

Example 1: Basic Usage of comment_form_logged_in Filter

In this example, we update the logged-in message for the comment form.

    function weplugins_modify_comment_form_logged_in_defaults($args_logged_in, $commenter, $user_identity) { 
        // Update the $args_logged_in variable according to your website requirements and return this variable.
        $args_logged_in = "Welcome back, " . $user_identity . "! Feel free to leave a comment.";
        return $args_logged_in; 
    }
    // Add the filter
    add_filter("comment_form_logged_in", "weplugins_modify_comment_form_logged_in_defaults", 10, 3);
    

Example 2: Conditional Message Based on User Role

This example modifies the logged-in message based on the user role.

    function weplugins_modify_comment_form_logged_in_defaults($args_logged_in, $commenter, $user_identity) { 
        if (current_user_can('editor')) {
            $args_logged_in = "Hello Editor, " . $user_identity . "! Your feedback is highly valued.";
        } else {
            $args_logged_in = "Hello " . $user_identity . ", thank you for your comment.";
        }
        return $args_logged_in; 
    }
    // Add the filter
    add_filter("comment_form_logged_in", "weplugins_modify_comment_form_logged_in_defaults", 10, 3);
    

Example 3: Removing the Filter

If you need to remove the filter, use the example below.

    // Remove the filter
    remove_filter("comment_form_logged_in", "weplugins_modify_comment_form_logged_in_defaults", 10, 3);
    

Please make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.

Access Premium WordPress Plugins

Contact Us

If you need customization or help with this hook, feel free to contact us.

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.