This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
Alright, so you’re diving into the world of WordPress hooks, and today we’re focusing on the delete_comment action hook. This hook is super handy because it lets you execute custom code right before a comment is deleted from your site’s database. Whether you’re adding this to your theme’s functions.php
or creating a custom plugin (which we highly recommend to keep things tidy during theme updates), the delete_comment action is a go-to for developers looking to customize comment deletion behavior.
Example 1: Adding Custom Functionality on Comment Deletion
Here’s a basic example that shows how you can use the delete_comment action. In this snippet, we define a function called weplugins_execute_on_delete_comment_event
that performs actions when a comment is about to be deleted.
function weplugins_execute_on_delete_comment_event($comment_id, $comment){ // Custom logic here } // Add the action add_action( "delete_comment", "weplugins_execute_on_delete_comment_event" , 10, 2);
Example 2: Removing the Action Hook
Sometimes, you might need to remove an action hook. Here’s how you can remove the delete_comment action using remove_action
.
remove_action( "delete_comment", "weplugins_execute_on_delete_comment_event", 10, 2 );
Example 3: Logging Deleted Comments
In this example, let’s log the details of a comment right before it’s deleted. This can be useful for auditing or maintaining records.
function weplugins_log_deleted_comment($comment_id, $comment){ error_log('Comment ID ' . $comment_id . ' is about to be deleted.'); } add_action( "delete_comment", "weplugins_log_deleted_comment" , 10, 2);
Contact Us
If you need any help or customization using the delete_comment action, feel free to Contact Us. We’re here to assist you with any WordPress development needs.
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.