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.
Hey there! If you’re working on a WordPress site and need to handle situations where comments are attempted on non-existent posts, the comment_id_not_found action hook is your friend. Let’s dive into how you can use this hook with some practical examples.
comment_id_not_found action
Fires when a comment is attempted on a post that does not exist.
To use the comment_id_not_found action, you first need to register it using add_action. You can write this code into the functions.php of your activated theme or in a custom WordPress Plugin. At WePlugins, we always recommend creating a custom WordPress Plugin when using hooks to avoid issues when updating your WordPress theme in the future.
In the example below, we define a function weplugins_execute_on_comment_id_not_found_event
which takes one parameter and is registered using add_action. The first parameter comment_id_not_found is the name of the hook, the second parameter weplugins_execute_on_comment_id_not_found_event is the name of the function 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 to the registered function.
Sometimes, you might need to remove a registered hook, so you can use remove_action to remove the comment_id_not_found action.
Parameters
Below is the 1 parameter required to use this hook:
- $comment_post_ID : (int) Post ID.
Live Examples
Example 1: Basic Hook Registration
Here’s a simple example of how you can use this hook:
function weplugins_execute_on_comment_id_not_found_event($comment_post_ID){ //You can write code here to be executed when this action occurs in WordPress. //Use the parameters received in the function arguments & implement the required //additional custom functionality according to your website requirements. } // add the action add_action( "comment_id_not_found", "weplugins_execute_on_comment_id_not_found_event" , 10, 1);
Example 2: Logging the Event
In this example, we’ll log the event when a comment is attempted on a non-existent post:
function weplugins_log_comment_id_not_found($comment_post_ID){ error_log("Comment attempted on post ID: " . $comment_post_ID); } // add the action add_action( "comment_id_not_found", "weplugins_log_comment_id_not_found" , 10, 1);
Example 3: Sending an Email Notification
This example will send an email notification to the admin when a comment is attempted on a non-existent post:
function weplugins_notify_admin_comment_id_not_found($comment_post_ID){ $admin_email = get_option('admin_email'); wp_mail($admin_email, "Comment Attempted on Non-Existent Post", "A comment was attempted on post ID: " . $comment_post_ID); } // add the action add_action( "comment_id_not_found", "weplugins_notify_admin_comment_id_not_found" , 10, 1);
To remove a hook callback, use the example below:
remove_action( "comment_id_not_found", "weplugins_execute_on_comment_id_not_found_event", 10, 1 );
Ensure you provide the same callback function name, priority, and number of arguments while removing the hook callback.
Contact Us
If you need any customization or run into issues using this hook, feel free to reach out to us at WePlugins Contact Page. We’re here to help!
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.