{"id":503,"date":"2020-10-20T23:23:58","date_gmt":"2020-10-21T04:23:58","guid":{"rendered":"http:\/\/badecho.com\/?p=503"},"modified":"2023-02-20T20:40:02","modified_gmt":"2023-02-21T01:40:02","slug":"automatically-posting-wordpress-posts-to-discord","status":"publish","type":"post","link":"https:\/\/badecho.com\/index.php\/2020\/10\/20\/automatically-posting-wordpress-posts-to-discord\/","title":{"rendered":"Automatically Posting WordPress Posts to Discord"},"content":{"rendered":"\n<h2>Manually Linking Articles on Discord is a Pain&#8230;<\/h2>\n\n\n\n<p>If you&#8217;re like me, you might run a <a href=\"https:\/\/discord.gg\/omni\" target=\"_blank\" rel=\"noreferrer noopener\">Discord server<\/a> 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.<\/p>\n\n\n\n<p>I expected to find a bunch of easy solutions from a simple Google search &#8212; 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 &#8212; so I&#8217;m going to show my method of automatically posting WordPress posts to my Discord.<\/p>\n\n\n\n<p>This will require you to be able to do a few things. Most importantly, you&#8217;ll need to be able to manually upload a plugin on your WordPress server. So, if you don&#8217;t have admin access to the machine or directory the site resides in &#8212; my solution won&#8217;t help you. I looked at what&#8217;s involved in order to upload a plugin and get it approved on the official WordPress website, and I&#8217;m not interested in dealing with that. Sorry!<\/p>\n\n\n\n<h2>Creating a Webhook in Discord<\/h2>\n\n\n\n<p>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&#8217;ve made your choice, right click on the channel to edit its settings.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" width=\"356\" height=\"361\" src=\"http:\/\/badecho.com\/wp-content\/uploads\/2020\/10\/DiscordEditChannel.png\" alt=\"Automatically posting WordPress posts to Discord will require us to edit our channel. Shows us clicking &quot;Edit Channel&quot; on the channel we want announcements in.\" class=\"wp-image-505\" srcset=\"https:\/\/badecho.com\/wp-content\/uploads\/2020\/10\/DiscordEditChannel.png 356w, https:\/\/badecho.com\/wp-content\/uploads\/2020\/10\/DiscordEditChannel-296x300.png 296w\" sizes=\"(max-width: 356px) 100vw, 356px\" \/><figcaption>Click &#8220;Edit Channel&#8221; to edit the announcement channel settings.<\/figcaption><\/figure><\/div>\n\n\n\n<p>Once we have the settings up for the channel, let&#8217;s navigate on over to the &#8220;Integrations&#8221; section, and then add a new Webhook.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" width=\"460\" height=\"108\" src=\"http:\/\/badecho.com\/wp-content\/uploads\/2020\/10\/DiscordNewWebhook.png\" alt=\"Shows us clicking on the New Webhook button to create our new WordPress Webhook\" class=\"wp-image-507\" srcset=\"https:\/\/badecho.com\/wp-content\/uploads\/2020\/10\/DiscordNewWebhook.png 460w, https:\/\/badecho.com\/wp-content\/uploads\/2020\/10\/DiscordNewWebhook-300x70.png 300w\" sizes=\"(max-width: 460px) 100vw, 460px\" \/><figcaption>Click on the &#8220;Integrations&#8221; tab and then the &#8220;New Webhook&#8221; button to create your new WordPress Webhook.<\/figcaption><\/figure><\/div>\n\n\n\n<p>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&#8217;ll need it later.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" width=\"661\" height=\"305\" src=\"http:\/\/badecho.com\/wp-content\/uploads\/2020\/10\/DiscordWebhookCopy.png\" alt=\"Shows us clicking on &quot;Copy Webhook URL&quot; in our new Webhook.\" class=\"wp-image-506\" srcset=\"https:\/\/badecho.com\/wp-content\/uploads\/2020\/10\/DiscordWebhookCopy.png 661w, https:\/\/badecho.com\/wp-content\/uploads\/2020\/10\/DiscordWebhookCopy-300x138.png 300w, https:\/\/badecho.com\/wp-content\/uploads\/2020\/10\/DiscordWebhookCopy-480x221.png 480w\" sizes=\"(max-width: 661px) 100vw, 661px\" \/><figcaption>Name the Webhook bot what you want, give it an icon maybe, and click the &#8220;Copy Webhook URL&#8221;.<\/figcaption><\/figure><\/div>\n\n\n\n<p>That&#8217;s it! Everything you need to do on Discord is done. You&#8217;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.<\/p>\n\n\n\n<h2>Adding the Post to Discord Plugin to WordPress<\/h2>\n\n\n\n<p>Now you just need to upload this little plugin I wrote to your <code>\/wp-content\/plugins<\/code> directory. Create a folder there named <code>post-to-discord<\/code> (or whatever you want), and in there create a file named <code>post-to-discord.php<\/code> with the following code in it:<\/p>\n\n\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php\n\/*\n * Plugin Name: Post to Discord\n * Plugin URI: https:\/\/badecho.com\n * Description: Announces new WordPress posts on Discord.\n * Version: 1.0\n * Author: Matt Weber\n * Author URI: https:\/\/badecho.com\n*\/\n\nfunction post_to_discord($new_status, $old_status, $post) {\t\n\tif(get_option('discord_webhook_url') == null) \n\t\treturn;\n\t \n\tif ( $new_status != 'publish' || $old_status == 'publish' || $post-&gt;post_type != 'post')\n\t\treturn;\n\n\t$webhookURL = get_option('discord_webhook_url');\n\t$id = $post-&gt;ID;\n\n\t$author = $post-&gt;post_author;\n\t$authorName = get_the_author_meta('display_name', $author);\n\t$postTitle = $post-&gt;post_title;\n\t$permalink = get_permalink($id);\n\t$message = &quot;@everyone &quot; . $authorName . &quot; just posted \\&quot;&quot; . $postTitle . &quot;\\&quot; for your reading pleasure: &quot; . $permalink;\n\n\t$postData = array('content' =&gt; $message);\n\n\t$curl = curl_init($webhookURL);\n\tcurl_setopt($curl, CURLOPT_CUSTOMREQUEST, &quot;POST&quot;);\t\n\tcurl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($postData));\n\tcurl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application\/json'));\n\tcurl_setopt($curl, CURLOPT_RETURNTRANSFER, true);\n\tcurl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);\n\tcurl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);\n\t\n\t$response = curl_exec($curl);\n\t$errors = curl_error($curl);\t\t\n\t\n\tlog_message($errors);\n}\n\nfunction log_message($log) {\n\t  if (true === WP_DEBUG) {\n            if (is_array($log) || is_object($log)) {\n                error_log(print_r($log, true));\n            } else {\n                error_log($log);\n            }\n        }\n}\n\nadd_action('transition_post_status', 'post_to_discord', 10, 3);\n\nfunction post_to_discord_section_callback() {\n  echo &quot;&lt;p&gt;A valid Discord Webhook URL to the announcements channel is required.&quot;;\n}\n\nfunction post_to_discord_input_callback() {\n\n  echo '&lt;input name=&quot;discord_webhook_url&quot; id=&quot;discord_webhook_url&quot; type=&quot;text&quot; value=&quot;' . get_option('discord_webhook_url') . '&quot;&gt;';\n}\n\nfunction post_to_discord_settings_init() {\n add_settings_section(\n   'discord_webhook_url',\n   'Post to Discord',\n   'post_to_discord_section_callback',\n   'general'\n );\n\n add_settings_field(\n   'discord_webhook_url',\n   'Discord Webhook URL',\n   'post_to_discord_input_callback',\n   'general',\n   'discord_webhook_url'\n );\n\n register_setting( 'general', 'discord_webhook_url' );\n}\n\nadd_action( 'admin_init', 'post_to_discord_settings_init' );\n<\/pre>\n\n\n<p>After you have done this, go to your <strong>Plugins<\/strong> page on WordPress and activate the <strong>Post to Discord<\/strong> plugin. Finally, go to <strong>Settings -&gt; General<\/strong> and scroll down to input the Webhook URL we copied earlier into the input box added by the plugin.<\/p>\n\n\n\n<p>Please be advised that of all the computer languages and technologies out there, I am <em>least<\/em> knowledgeable about this beast known as PHP! So, if I&#8217;m committing any cardinal sins with it, I apologize! It works for me though, and hopefully it works for you.<\/p>\n\n\n\n<p>This should cause a &#8220;ping&#8221; 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 <strong>Customize<\/strong> page.<\/p>\n\n\n\n<p>Happy writing and happy pinging!<\/p>\n\n\n\n<p><em>~Omni<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Manually Linking Articles on Discord is a Pain&#8230; If you&#8217;re like me, you might run a Discord server where like-minded [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[10],"tags":[30,32,31],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v14.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\r\n<title>Automatically Posting WordPress Posts to Discord - omni&#039;s hackpad<\/title>\r\n<meta name=\"description\" content=\"Manually notifying my viewers about new hackpad posts is a pain, so here&#039;s my way of automatically posting new Wordpress posts to Discord.\" \/>\r\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/badecho.com\/index.php\/2020\/10\/20\/automatically-posting-wordpress-posts-to-discord\/\" \/>\r\n<meta property=\"og:locale\" content=\"en_US\" \/>\r\n<meta property=\"og:type\" content=\"article\" \/>\r\n<meta property=\"og:title\" content=\"Automatically Posting WordPress Posts to Discord - omni&#039;s hackpad\" \/>\r\n<meta property=\"og:description\" content=\"Manually notifying my viewers about new hackpad posts is a pain, so here&#039;s my way of automatically posting new Wordpress posts to Discord.\" \/>\r\n<meta property=\"og:url\" content=\"https:\/\/badecho.com\/index.php\/2020\/10\/20\/automatically-posting-wordpress-posts-to-discord\/\" \/>\r\n<meta property=\"og:site_name\" content=\"omni&#039;s hackpad\" \/>\r\n<meta property=\"article:published_time\" content=\"2020-10-21T04:23:58+00:00\" \/>\r\n<meta property=\"article:modified_time\" content=\"2023-02-21T01:40:02+00:00\" \/>\r\n<meta property=\"og:image\" content=\"http:\/\/badecho.com\/wp-content\/uploads\/2020\/10\/DiscordEditChannel.png\" \/>\r\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\r\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/badecho.com\/#website\",\"url\":\"https:\/\/badecho.com\/\",\"name\":\"omni&#039;s hackpad\",\"description\":\"Game Code Disassembly. Omnified Modification. Madness.\",\"publisher\":{\"@id\":\"https:\/\/badecho.com\/#\/schema\/person\/3de67496328be7ae6e1f52faf582e9d2\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/badecho.com\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/badecho.com\/index.php\/2020\/10\/20\/automatically-posting-wordpress-posts-to-discord\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"http:\/\/badecho.com\/wp-content\/uploads\/2020\/10\/DiscordEditChannel.png\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/badecho.com\/index.php\/2020\/10\/20\/automatically-posting-wordpress-posts-to-discord\/#webpage\",\"url\":\"https:\/\/badecho.com\/index.php\/2020\/10\/20\/automatically-posting-wordpress-posts-to-discord\/\",\"name\":\"Automatically Posting WordPress Posts to Discord - omni&#039;s hackpad\",\"isPartOf\":{\"@id\":\"https:\/\/badecho.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/badecho.com\/index.php\/2020\/10\/20\/automatically-posting-wordpress-posts-to-discord\/#primaryimage\"},\"datePublished\":\"2020-10-21T04:23:58+00:00\",\"dateModified\":\"2023-02-21T01:40:02+00:00\",\"description\":\"Manually notifying my viewers about new hackpad posts is a pain, so here's my way of automatically posting new Wordpress posts to Discord.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/badecho.com\/index.php\/2020\/10\/20\/automatically-posting-wordpress-posts-to-discord\/\"]}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/badecho.com\/index.php\/2020\/10\/20\/automatically-posting-wordpress-posts-to-discord\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/badecho.com\/index.php\/2020\/10\/20\/automatically-posting-wordpress-posts-to-discord\/#webpage\"},\"author\":{\"@id\":\"https:\/\/badecho.com\/#\/schema\/person\/3de67496328be7ae6e1f52faf582e9d2\"},\"headline\":\"Automatically Posting WordPress Posts to Discord\",\"datePublished\":\"2020-10-21T04:23:58+00:00\",\"dateModified\":\"2023-02-21T01:40:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/badecho.com\/index.php\/2020\/10\/20\/automatically-posting-wordpress-posts-to-discord\/#webpage\"},\"commentCount\":11,\"publisher\":{\"@id\":\"https:\/\/badecho.com\/#\/schema\/person\/3de67496328be7ae6e1f52faf582e9d2\"},\"image\":{\"@id\":\"https:\/\/badecho.com\/index.php\/2020\/10\/20\/automatically-posting-wordpress-posts-to-discord\/#primaryimage\"},\"keywords\":\"Discord,PHP,WordPress\",\"articleSection\":\"General Dev\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/badecho.com\/index.php\/2020\/10\/20\/automatically-posting-wordpress-posts-to-discord\/#respond\"]}]},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/badecho.com\/#\/schema\/person\/3de67496328be7ae6e1f52faf582e9d2\",\"name\":\"Matt Weber\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/badecho.com\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/7e345ac2708b3a41c7bd70a4a0440d41?s=96&d=mm&r=g\",\"caption\":\"Matt Weber\"},\"logo\":{\"@id\":\"https:\/\/badecho.com\/#personlogo\"}}]}<\/script>\r\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/badecho.com\/index.php\/wp-json\/wp\/v2\/posts\/503"}],"collection":[{"href":"https:\/\/badecho.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badecho.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badecho.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badecho.com\/index.php\/wp-json\/wp\/v2\/comments?post=503"}],"version-history":[{"count":7,"href":"https:\/\/badecho.com\/index.php\/wp-json\/wp\/v2\/posts\/503\/revisions"}],"predecessor-version":[{"id":522,"href":"https:\/\/badecho.com\/index.php\/wp-json\/wp\/v2\/posts\/503\/revisions\/522"}],"wp:attachment":[{"href":"https:\/\/badecho.com\/index.php\/wp-json\/wp\/v2\/media?parent=503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badecho.com\/index.php\/wp-json\/wp\/v2\/categories?post=503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badecho.com\/index.php\/wp-json\/wp\/v2\/tags?post=503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}