-
-
Save ntwb/77e158f8f190455b3f06 to your computer and use it in GitHub Desktop.
post_to_slack.php
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 | |
/** | |
* 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