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 send_comment_by_email that will be trigger by the comment_post hook (fires immediately after a comment is inserted into the database):
<?php
/**
* by ShellHacks
* WordPress script: send-comment-by-email
* Copyright (c) 2011-2024 www.ShellHacks.com <mail@shellhacks.com>
*
* Send new WordPress comments by email
* Documentation: https://www.shellhacks.com/wordpress-comment-hook-trigger-action-example
* Source: https://github.com/shellhacks/WordPress/blob/main/src/send-comment-by-email.php
*
* Tested on WordPress, version=6.4.2
* Version: 1.0.0
*/
function send_comment_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', 'send_comment_by_email', 10, 2 );
?>
The latest version of this script can be found on GitHub.
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 send_comment_to_telegram that sends new comments on your WordPress site to the Telegram channel:
<?php
/**
* by ShellHacks
* WordPress script: send-comment-to-telegram
* Copyright (c) 2011-2024 www.ShellHacks.com <mail@shellhacks.com>
*
* Send new WordPress comments to Telegram
* Documentation: https://www.shellhacks.com/wordpress-comment-hook-trigger-action-example
* Source: https://github.com/shellhacks/WordPress/blob/main/src/send-comment-to-telegram.php
*
* Tested on WordPress, version=6.4.2
* Version: 1.0.0
*/
function send_comment_to_telegram( $comment_id, $comment_approved ) {
if ( ! $comment_approved ) {
$apiToken = "0123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghi";
$comment = get_comment( $comment_id );
$comment_parent_id = $comment->comment_parent;
if ( $comment_parent_id != 0) {
$comment_parent = get_comment( $comment_parent_id );
$comment_parent_author = $comment_parent->comment_author;
};
$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 );
if ( $comment_parent_id != 0) {
$message_header = sprintf( "↩️ by <em>%s</em> to <a href=\"%s/#comment-%s\"><em>%s</em></a>:", $comment_author,
$post_url,
$comment_parent_id,
$comment_parent_author);
} else {
$message_header = sprintf( "💬 by <em>%s</em>:", $comment_author);
};
$message = sprintf( "%s\n<blockquote>%s</blockquote>\n🌐 <a href=\"%s\">%s</a>", $message_header,
$comment_content,
$post_url,
$post_title );
$data = [
'chat_id' => '012345678',
'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', 'send_comment_to_telegram', 10, 2 );
?>
The latest version of this script can be found on GitHub.
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 →