Wordpress Webhooks to Sync Posts

Suppose you decided to sync Wordpress with externaly system

Here is a quick and easy way to do so without touchind Wordpress internals

# docker network rm my

# all containers will be in same network for connectivity
docker network create my

# sample echo server
docker run -it --rm --name=hook --network=my -p 8000:80 mendhak/http-https-echo

# because of Wordpress plugin does not want to work with ports and non https
docker run --rm -it --name=ngrok --network=my wernight/ngrok ngrok http hook:80

# database
docker run -it --rm --name=db --network=my -e MYSQL_DATABASE=demo -e MYSQL_USER=demo -e MYSQL_PASSWORD=demo -e MYSQL_RANDOM_ROOT_PASSWORD=1 mysql:5.7

# wordpress
docker run -it --rm --name=wp -p 8080:80 --network=my -e WORDPRESS_DB_HOST=db -e WORDPRESS_DB_USER=demo -e WORDPRESS_DB_PASSWORD=demo -e WORDPRESS_DB_NAME=demo wordpress

Navigate to localhost:8080, setup wordpress by pressing next, next, finish. Then go to its plugin wind and install wp webhooks, activate it and put ngrock link into it

image

image

image

The main lack of this plugin is that it sends raw content without processing it, so were forced to figure out how to write my own and it was pretty easy

<?php
/*
Plugin Name: machook
*/
function machook($post_ID) {
    $post = get_post($post_ID);
    $html = apply_filters('the_content', $post->post_content);
    $context = stream_context_create([
        'http' => [
            'method' => 'POST',
            'content' => json_encode([
                'post' => $post,
                'html' => $html
            ]),
            'header' => 'Content-Type: application/json'
        ]
    ]);
    $result = file_get_contents('http://ee1e-178-150-44-191.ngrok.io/', false, $context);
    // $response = json_decode($result); // who cares 🤷‍♂️
}
add_action('post_updated', 'machook');