Post to Discord from Wordpress

Posted on Sun 07 March 2021 in Programming

The bulbcalculator project has now a Discord server, hoping it would be used sometimes in the future. Anyway, just to play around, I wanted to be able to post on a channel of the Discord server when posting something on the project site, basically announces.

It turned out that the process is really simple, you just need to set a webhook on the Discord server, tied to the channel you want, and set a simple plugin on the Wordpress site.

Now, for the webhook on Discord there was a lot of documentation, so it is really simple. For the Wordpress side things are a little less documented, but I had the luck to find this plugin from Matt Weber that does just what I need. The only problem from my point of view is that it forward to Discord every post while I just wanted to forward post form a certain category.

Luckily Matt's plugin is really simple and so I just needed to extend it to consider the category of the post, which after some tinkering ended up to be really simple. The end result is wp2discord, a Wordpress plugin which forward the post of a certain category to a given Discord channel (on a given server of course). The only thins you need to configure is the webhook id and input the category for which it must be done.

The plugin is basically a copy of the original work from Matt, to which I give credit and I must thank a lot, with two important changes:

  • the first one is the different "action" it intercept, the original get the transition_post_status action, while I ended up intercepting the publish_post one.

  • as a consequence of the different action intercepted, I am now able to get also the category (or categories) of the post to check if the correct one is present

The plugin then loop over every category of the post and if it find the correct one is found the post is forwarded as a message to the Discord channel.
The message itself contain the title and the permalink of the post.

The only thing I'd like to add is to be able to post to different channels based on the category of the post, but is not really urgent for now.