Skip to content

Instantly share code, notes, and snippets.

@Archie22is
Created August 6, 2019 09:00
Show Gist options
  • Save Archie22is/56f2799338d781a3724f69e82544b702 to your computer and use it in GitHub Desktop.
Save Archie22is/56f2799338d781a3724f69e82544b702 to your computer and use it in GitHub Desktop.
Send a notification or ping in WordPress when a post is created
<?php
/**
* @param int $post_id The ID of the post.
* @param WP_Post $post The post object.
* @param bool $update True if the post already exists and is being updated
*/
function wpse_post_ping( $post_id, $post, $update ) {
if ( $post->post_status === 'publish' ) { // Only fire when published
wp_remote_post(
'http://example.com/application/notify',
array(
// https://codex.wordpress.org/HTTP_API
'blocking' => false, // If you don't need to know the response, disable blocking for an "async" request
'body' => array(
'post_id' => $post_id,
'post' => json_encode( $post ),
'update' => ( int ) $update,
// or whatever
)
)
);
}
}
add_action( 'wp_insert_post', 'wpse_post_ping', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment