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

How to use pre_comment_on_post action in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
November 26, 2022
5 minutes read

pre_comment_on_post action

Fires before a comment is posted.

To use pre_comment_on_post action, first you have to register it using add_action. 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_execute_on_pre_comment_on_post_event which takes 1 parameter and we registered using add_action. The first parameter pre_comment_on_post is the name of the hook, The second parameter weplugins_execute_on_pre_comment_on_post_event 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_action to remove pre_comment_on_post action.

Parameters

    Below the 1 parameter is required to use this hook.

  • $comment_post_ID : (int) Post ID.

Live Example 1: Adding a custom message before comment is posted

This example demonstrates how to display a custom message before a comment is posted on a WordPress post.

	function weplugins_execute_on_pre_comment_on_post_event($comment_post_ID){
		// Add your custom code here
		echo "A new comment is being posted on post ID: " . $comment_post_ID;
	}
	// add the action
	add_action( "pre_comment_on_post", "weplugins_execute_on_pre_comment_on_post_event" , 10, 1);
	

Live Example 2: Logging comment post ID before comment is posted

This example shows how to log the comment post ID into a custom log file before a comment is posted.

	function weplugins_log_pre_comment_post_id($comment_post_ID){
		// Log the comment post ID
		error_log("Comment is being posted on post ID: " . $comment_post_ID, 3, "/var/log/weplugins_comments.log");
	}
	// add the action
	add_action( "pre_comment_on_post", "weplugins_log_pre_comment_post_id" , 10, 1);
	

Live Example 3: Validate comment content before posting

This example validates the content of the comment before it is posted.

	function weplugins_validate_comment_content($comment_post_ID){
		// Custom validation logic
		$comment_content = $_POST['comment'];
		if (empty($comment_content) || strlen($comment_content) < 10) {
			wp_die('Comment content must be at least 10 characters long.');
		}
	}
	// add the action
	add_action( "pre_comment_on_post", "weplugins_validate_comment_content" , 10, 1);
	&#91;/php&#93;
	
	<p>To remove a hook callback, use the example below.</p>
	
	function weplugins_remove_pre_comment_hook(){
		remove_action( "pre_comment_on_post", "weplugins_execute_on_pre_comment_on_post_event", 10, 1 );
	}
	

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 any customization or assistance using 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.