Last active
August 29, 2015 13:57
-
-
Save benjick/9618184 to your computer and use it in GitHub Desktop.
Send new Wordpress posts to Slack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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