Search code examples
phphtmlwordpressslackslack-api

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 send the content of the post to the channel including the formatting and HTML. I can easily get the content to post to the channel, but I'm struggling to get the content to be formatted. This may be a Slack issue, but I'm not sure.

Here's the code.

`$message = apply_filters('the_content',$post->post_content);`

I'm not sure if this is even possible, but I thought I'd ask.


Solution

  • 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.