Skip to content

Instantly share code, notes, and snippets.

@benjick
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjick/9618184 to your computer and use it in GitHub Desktop.
Save benjick/9618184 to your computer and use it in GitHub Desktop.
Send new Wordpress posts to Slack
<?php
# based on https://gist.github.com/alexstone/9319715
function slack_post($post_id) {
if( ( $_POST['post_status'] == 'publish' ) && ( $_POST['original_post_status'] != 'publish' ) ) {
$room = "general"; // What Slack channel to send to
$post = get_post($post_id);
$author = get_userdata($post->post_author);
$message = "New post '" . get_the_title($post_id) . "' by {$author->user_firstname} - " . get_permalink($post->ID);
$data = "payload=" . json_encode(array(
"channel" => "#{$room}",
"text" => $message,
"icon_emoji" => ":globe_with_meridians:",
"username" => "Wordpress"
));
$ch = curl_init("https://TEAM.slack.com/services/hooks/incoming-webhook?token=TOKEN"); // Put your Incoming WebHooks URL here
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
ob_start(); // no output pls
$result = curl_exec($ch);
curl_close($ch);
ob_end_clean();
}
}
add_action( 'publish_post', 'slack_post' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment