Skip to content

Instantly share code, notes, and snippets.

@AaronRutley
Last active March 11, 2020 00:07
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AaronRutley/c8433456a734d4d42bf3 to your computer and use it in GitHub Desktop.
Save AaronRutley/c8433456a734d4d42bf3 to your computer and use it in GitHub Desktop.
post_to_slack.php
<?php
/**
* Post a message to Slack from WordPress
*
* @param string $message the message to be sent to Slack
* @param string $channel the #channel to send the message to (or @user for a DM)
* @param string $username the username for this bot eg : WordPress bot
* @param string $icon_emoji the icon emoji name for this bot eg :monkey:
*
* @link slack incoming webhook docs https://api.slack.com/incoming-webhooks
* @example ar_post_to_slack('message','#channel','bot-name',':monkey:');
*/
function ar_post_to_slack($message, $channel, $username, $icon_emoji) {
// Slack webhook endpoint from Slack settings
$slack_endpoint = "https://hooks.slack.com/services/your-webhook-endpoint-here";
// Prepare the data / payload to be posted to Slack
$data = array(
'payload' => json_encode( array(
"channel" => $channel,
"text" => $message,
"username" => $username,
"icon_emoji" => $icon_emoji
)
)
);
// Post our data via the slack webhook endpoint using wp_remote_post
$posting_to_slack = wp_remote_post( $slack_endpoint, array(
'method' => 'POST',
'timeout' => 30,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => array(),
'body' => $data,
'cookies' => array()
)
);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment