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.

Automatically posting WordPress posts to Discord will require us to edit our channel. Shows us clicking "Edit Channel" on the channel we want announcements in.
Click “Edit Channel” to edit the announcement channel 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.

Shows us clicking on the New Webhook button to create our new WordPress Webhook
Click on the “Integrations” tab and then the “New Webhook” button to create your new WordPress 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.

Shows us clicking on "Copy Webhook URL" in our new Webhook.
Name the Webhook bot what you want, give it an icon maybe, and click the “Copy Webhook URL”.

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