Skip to content

Instantly share code, notes, and snippets.

@dgaitan
Last active July 10, 2022 19:38
Show Gist options
  • Save dgaitan/10f1b7bec9f327cfea9384c092faeec6 to your computer and use it in GitHub Desktop.
Save dgaitan/10f1b7bec9f327cfea9384c092faeec6 to your computer and use it in GitHub Desktop.
WordPress Hooks
<?php
/**
* See the following link to know the arguments passed by this action.
*
* @see https://developer.wordpress.org/reference/hooks/post_updated/
* @param int $post_ID - the post ID updated
* @param WP_Post $post_after - Post content after updated
* @param WP_Post $post_before - Post content before updated.
*/
function notify_users_that_post_was_updated( $post_ID, $post_after, $post_before ) {
$users_email = array(
'john@doe.com',
'foo@bar.com',
'juan@perez.com
);
// Let's prepare the email body.
$body = sprintf(
'Hi all! This is a quick notification that the post %s was updated. <a href="%s">You can read it now</a>',
$post_after->post_title,
get_the_permalink( $post_after )
);
/**
* See the link to understand how the wp_mail function works
*
* @see https://developer.wordpress.org/reference/functions/wp_mail/
*/
wp_mail(
$users_email,
"A post was updated in my amazing blog",
$body
);
}
/**
* Note that this action hook receives three params. So we need to let to WordPress
* know that we need to get the three arguments, otherwise it will broke.
*/
add_action( 'post_updated', 'notify_users_that_post_was_updated', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment