Skip to content Skip to sidebar Skip to footer

Wordpress Post Notification With Full HTML Via Slack

When I create a post in Wordpress a slack notification plugin sends notifications to a channel using the slack api. The notification works fine. What I'm trying to do is to the sen

Solution 1:

try referring to this repo it might help i guess you can just change some of the code or get something on there that might help.

github/wp-slack-notification

Edit:

If you want to notify slack with similar content of your posts then it is not possible because slack only accepts json with specific structuring and keys. Also slack does not accept markdown.

reference for structuring contents: Block Kit Builder

If you try image or pdf version of content you want to notify it might be small in size.

I suggest, why not get the post contents using wordpress provided functions then pass it in a json format using json_encode and use Block Kit Builder if what you want contents will look like.

$data_encoded = array(
    "payload" => json_encode(create_message())
  );
    $post_on_slack = wp_remote_post($webhook_endpoint, array(
      'method' => 'POST',
      'headers' => array(),
      'body' => $data_encoded,
    )
  );

for the $webhook_endpoint you can refer to the repo on how to get it.

function create_message() {
$data = array(
    // structure here depends on what your Block Kit Builder
);
return $data;
}

The first Block of Code I created can be wrapped into function so that you can call it via hooks something like

add_action("save_post","you_function");


Solution 2:

Slack does currently only support markup formatting using a Slack specific markup language.

It does not support messages formatted in HTML or provide any automatic conversion of HTML to Slack markup (which btw. would not also not make much sense, since Slack's markup only has basic formatting features, not nothing advanced like tables, font sizes, etc.) Plus you can add attachments, which give you some additional formatting options like adding images.

In addition Slack support a more sophisticated formatting with so called "posts". However, posts are not supported by the API, so no help here either.

That leaves you with three options:

  • Convert the HTML of your posts to Slack markup plus attachments
  • Convert the HTML of your posts to an image and then post that on Slack
  • Convert the HTML of your posts to PDF and then post that on Slack

AFAIK there are plenty of existing converters for HTML to images or PDF, so I would suggest the 2nd or 3rd option.

Both image and PDFs will be displayed within the channel and Slack has it's own viewer to show details. Images are easier to handle, but PDFs gives you more options and a better rendering result. PDFs would also retain links in the HTML.


Post a Comment for "Wordpress Post Notification With Full HTML Via Slack"