Manually Linking Articles on Discord is a Pain…
If you’re like me, you might run a Discord server where like-minded people tend to congregate, relying on it for receiving notifications and updates regarding your latest activities. If you happen to also write WordPress articles, you probably want to share them with your Discord members. Manually having to share every article is a pain; automatically posting WordPress posts to Discord was something I wanted almost right away.
I expected to find a bunch of easy solutions from a simple Google search — there are lots of solutions available for automatically posting new Tweets and YouTube videos and all the other useless content people make to Discord. I could not find anything simple that worked out of the box when it came to WordPress however — so I’m going to show my method of automatically posting WordPress posts to my Discord.
This will require you to be able to do a few things. Most importantly, you’ll need to be able to manually upload a plugin on your WordPress server. So, if you don’t have admin access to the machine or directory the site resides in — my solution won’t help you. I looked at what’s involved in order to upload a plugin and get it approved on the official WordPress website, and I’m not interested in dealing with that. Sorry!
Creating a Webhook in Discord
The first thing we need to do is set up an integration point on our Discord server. Go to your server and find the channel you want the announcements to appear in. Once you’ve made your choice, right click on the channel to edit its settings.

Once we have the settings up for the channel, let’s navigate on over to the “Integrations” section, and then add a new Webhook.

A new section will appear for the Webhook we just made. Give it a little bit of customization, but most importantly: grab that Webhook URL! We’ll need it later.

That’s it! Everything you need to do on Discord is done. You’ve actually just created a bot of sorts. It will just sit there doing nothing, however, until we make use of that Webhook URL we just copied.
Adding the Post to Discord Plugin to WordPress
Now you just need to upload this little plugin I wrote to your /wp-content/plugins
directory. Create a folder there named post-to-discord
(or whatever you want), and in there create a file named post-to-discord.php
with the following code in it:
<?php /* * Plugin Name: Post to Discord * Plugin URI: https://badecho.com * Description: Announces new WordPress posts on Discord. * Version: 1.0 * Author: Matt Weber * Author URI: https://badecho.com */ function post_to_discord($new_status, $old_status, $post) { if(get_option('discord_webhook_url') == null) return; if ( $new_status != 'publish' || $old_status == 'publish' || $post->post_type != 'post') return; $webhookURL = get_option('discord_webhook_url'); $id = $post->ID; $author = $post->post_author; $authorName = get_the_author_meta('display_name', $author); $postTitle = $post->post_title; $permalink = get_permalink($id); $message = "@everyone " . $authorName . " just posted \"" . $postTitle . "\" for your reading pleasure: " . $permalink; $postData = array('content' => $message); $curl = curl_init($webhookURL); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($postData)); curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); $response = curl_exec($curl); $errors = curl_error($curl); log_message($errors); } function log_message($log) { if (true === WP_DEBUG) { if (is_array($log) || is_object($log)) { error_log(print_r($log, true)); } else { error_log($log); } } } add_action('transition_post_status', 'post_to_discord', 10, 3); function post_to_discord_section_callback() { echo "<p>A valid Discord Webhook URL to the announcements channel is required."; } function post_to_discord_input_callback() { echo '<input name="discord_webhook_url" id="discord_webhook_url" type="text" value="' . get_option('discord_webhook_url') . '">'; } function post_to_discord_settings_init() { add_settings_section( 'discord_webhook_url', 'Post to Discord', 'post_to_discord_section_callback', 'general' ); add_settings_field( 'discord_webhook_url', 'Discord Webhook URL', 'post_to_discord_input_callback', 'general', 'discord_webhook_url' ); register_setting( 'general', 'discord_webhook_url' ); } add_action( 'admin_init', 'post_to_discord_settings_init' );
After you have done this, go to your Plugins page on WordPress and activate the Post to Discord plugin. Finally, go to Settings -> General and scroll down to input the Webhook URL we copied earlier into the input box added by the plugin.
Please be advised that of all the computer languages and technologies out there, I am least knowledgeable about this beast known as PHP! So, if I’m committing any cardinal sins with it, I apologize! It works for me though, and hopefully it works for you.
This should cause a “ping” to go off on your Discord server when you publish a new post, and it should only go off for posts that are indeed new. It also will filter out instances of the strange temporary post type that WordPress generates when you customize your CSS via the Customize page.
Happy writing and happy pinging!
~Omni
Hi,
a version with some control over which posts will be forwarded to Discord, based on the category of the post, is available at https://gitlab.com/mgian/wp2discord.
It is heavily based on Omni’s code written in this article.
Neat! Thanks for improving upon the original — this looks good for anyone who wants a bit more control over what gets posted to the Discord.
This plugin works great! Thank you! But can you make a version that doesn’t @everyone in the server? Some of my members are finding it annoying.
Hey Angela! I’m going to assume that some or many of my members are finding it a tad bit annoying too, haha.
Here’s some code that will just post without the dreaded @everyone:
WordPress to Discord – Without @everyone
Thank you so much, Matt! I overwrote the old code. Once we have a new post on the blog, I’ll let you know how it went! š
AMAZING!!!
Worked perfectly, thank you
Hey!!!
I am trying to make one for the post which are updated.
Like when we modify the post, is it possible for it to be pinged in discord?
On line 15 of the code, there is a condition that filters out updates made to existing posts (
|| $old_status == 'publish'
).If you remove that code, it should do a ping on updates. Haven’t tested it though.