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

How to use deleted_comment action in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
March 24, 2023
5 minutes read

Welcome to our detailed guide on the deleted_comment action in WordPress. This hook fires immediately after a comment is deleted from the database. Using hooks like this ensures your site remains flexible and up-to-date. Let’s dive into how you can use this to enhance your WordPress site.

To use the deleted_comment action, you first need to register it using add_action. You can add this code to your theme’s functions.php file or, preferably, in a custom WordPress Plugin. WePlugins always suggests using a custom plugin to prevent issues during theme updates.

Sometimes, you might want to remove a registered hook, and you can do that easily using remove_action.

Parameters:

  • $comment_id: (string) The comment ID as a numeric string.
  • $comment: (WP_Comment) The deleted comment.

Access Premium WordPress Plugins

Example 1: Executing a Function on Comment Deletion

Below is an example of how you can use this hook in your WordPress site.

    function weplugins_execute_on_deleted_comment_event($comment_id, $comment){
        // Code to execute when a comment is deleted
    }
    add_action("deleted_comment", "weplugins_execute_on_deleted_comment_event", 10, 2);
    

Example 2: Removing a Hook Callback

To remove a hook callback, use the example below.

    remove_action("deleted_comment", "weplugins_execute_on_deleted_comment_event", 10, 2);
    

Ensure you provide the same callback function name, priority, and number of arguments when removing the hook.

Example 3: Logging Deleted Comments

Here’s how you could log details of deleted comments for review.

    function weplugins_log_deleted_comment($comment_id, $comment){
        error_log("Comment ID $comment_id deleted: " . print_r($comment, true));
    }
    add_action("deleted_comment", "weplugins_log_deleted_comment", 10, 2);
    

For any customization needs or if you encounter issues using this hook, feel free to Contact Us. Our team at WePlugins is here to help!

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.