Glitch Chronicles

Glitch Chronicles – behind the scenes

Published: (Updated: ) in M's rambles, , , , by .

We have wanted to try writing and sharing with a broader public for some time, few days ago, we said to hell with all the planning – Just Do It™.

The blog is nothing special; our style is to usually make something distinctly ours – but that was only going to allow us to procrastinate further, and maybe never start.

So, 5 minutes later we had this WordPress up and running, chose a simple theme from the marketplace and my buddy Yuriy started posting.

For me it is a bit of a challenge to write something without having a prompt; so I used the first thing I saw – small WordPress plugin for posting to our Discord webhook.

Personally I haven’t touched php in close to a decade, and WordPress in even more time, but after spending some minutes searching for a plugin and not finding one, I decided to make one.

It took me nearly the same time searching for one, to just make one and I was nicely surprised to see that WordPress has kept it reputation over the years – still a breeze to work with and extend; sadly, now I have to again keep up to date with possible exploits and what not, so that is a minus in my book.

If you too are in need of a such plugin, feel free to use it – just throw this in a php file, zip it and upload it to your WordPress.

<?php
/*
 * Plugin Name: pub2wh
 * Description: Send published posts to a webhook.
 * Version: 2.0.0
 * Author: Martin
*/

function settings_init() {
    register_setting('general', 'webhook_url');

    add_settings_section(
        'webhook',
        'Webhook',
        function() { echo "Send new published posts to a webhook."; },
        'general'
    );

    add_settings_field(
        'webhook_url',
        'Webhook URL',
        function () { echo '<input id="webhook_url" name="webhook_url" type="text" value="' . get_option('webhook_url') . '">'; },
        'general',
        'webhook'
    );
}

add_action('admin_init', 'settings_init');

function send_post_to_webhook($id, $post) {
    if($post->post_modified <> $post->post_date) return;

    $hook = get_option('webhook_uri');
    if (!$hook) return;

    $message = "New post at Glitch Chronicles: " . get_permalink($id);

    $data = ["content" => $message];

    $curl = curl_init($hook);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    curl_exec($curl);

    curl_close($curl);
}

add_action('publish_post', 'send_post_to_webhook', 10, 2);

//p.s don't forget to change the message ;)

P.S: if you have opengraph meta data on your pages, the discord messages are especially nice

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *