WordPress: Comment Hook – Trigger Action Example

Actions are the hooks that the WordPress core launches at specific points during execution, or when specific events occur.

To trigger the action firstly you need to write a custom function known as a callback, and then register it with a WordPress hook using the add_action function in your child theme’s functions.php file.

This note shows the examples of how to trigger some action when a new comment is posted on a WordPress site, e.g. send an email or a message to a Telegram channel.

Cool Tip: How to send a message to a Telegram channel using PHP! Read more →

WordPress Comment Hook

Edit functions.php

The hooks and callback functions in the WordPress should be registered in a child theme’s functions.php file.

To edit this file, you can log in to your WordPress “Dashboard”, in the left sidebar hover over the “Appearance” and click on the “Theme Editor”.

Then, on the right, under the “Theme Files”, select the “Theme Functions (functions.php)”.

This will bring you up to the functions.php code editor.

Alternatively you can access the functions.php file over FTP or SSH on this path:

/wp-content/themes/<child_theme>/functions.php

WordPress do_action Function

Actions are the hooks that the WordPress core launches at specific points during execution, or when specific events occur.

To trigger an action firstly you need to write a custom function known as a callback, and then register it with a WordPress hook using the add_action function:

add_action( '<hook>', '<callback>', <priority>, <args> )
Argument Description
hook The name of the action to add the callback to (hooks list).
callback The callback to be run when the action is called.
priority Used to specify the order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution (default value: 10).
args The number of arguments the function accepts (default value: 1).

Trigger an Action on a Comment

As an example let’s create a callback function notify_by_email that will be trigger by the comment_post hook (fires immediately after a comment is inserted into the database):

<?php
  function notify_by_email( $comment_id, $comment_approved ) {
  if ( ! $comment_approved ) {
  $comment = get_comment( $comment_id );
  $mail = 'my_address@email.tld';
  $subject = sprintf( 'New Comment by: %s', $comment->comment_author );
  $message = $comment->comment_content;
  wp_mail( $mail, $subject, $message );
  }
  }
  add_action( 'comment_post', 'notify_by_email', 10, 2 );
?>

Cool Tip: How to send messages form the Linux command-line through the Telegram API! Create your personal notification bot! Read more →

Another example is the callback function notify_by_telegram that sends new comments on your WordPress site to the Telegram channel:

<?php
  function notify_by_telegram( $comment_id, $comment_approved ) {
  if ( ! $comment_approved ) {
  $apiToken = "5082654068:AAF7quCLZ4xuTq2FBdo3POssdJsM_FRHwTs";
  $comment = get_comment( $comment_id );
  $comment_author = $comment->comment_author;
  $comment_content = $comment->comment_content;
  $post_url = get_permalink( $comment->comment_post_ID );
  $post_title = get_the_title( $comment->comment_post_ID );
  $message = sprintf( "New &#128172; by <em>%s</em>:\n
<pre>%s</pre>\n
&#127760; <a href=\"%s\">%s</a>", $comment_author,
                                  $comment_content,
                                  $post_url,
                                  $post_title );
  $data = [
      'chat_id' => '515382482',
      'text' => $message,
      'parse_mode' => 'html',
      'disable_web_page_preview' => 'True'
  ];
  $response = file_get_contents("https://api.telegram.org/bot$apiToken/sendMessage?" .
                                 http_build_query($data) );
  }
  }
  add_action( 'comment_post', 'notify_by_telegram', 10, 2 );
?>

Here is how the new comments sent to the Telegram channel will look like:

To start using these hooks and callback functions, simply copy/paste the code above to your WordPress child theme’s functions.php file and replace the data in bold with your own.

In case of the error below, look at your php.ini, search for allow_url_fopen = Off and allow it by setting the value to On:

PHP Warning: file_get_contents(): https:// wrapper is disabled in the server configuration by allow_url_fopen=0 in <path>/functions.php on line <number>
PHP Warning: file_get_contents(https://api.telegram.org/…): failed to open stream: no suitable wrapper could be found in <path>/functions.php on line <number>

Cool Tip: How to find the location of the php.ini file! Read more →

Leave a Reply