How to trigger WordPress hooks in a multi-server setup?

Site runs on a backend (BE) WP server and several frontend (FE) servers.

FE has MySQL db r/o Slave of the BE, and WP installation with HyperDB plugin, so it reads from local, writes to the BE. W3TC plugin is used for caching on FE‘s.

Read More

New posts are created on the BE. Publishing these posts triggers hooks only on the BE.

Question: how can these hooks be triggered also on all the FE‘s to reset their caching?

p.s. I asked the similar question in W3TC plugin support forum a while ago, no replies.

Related posts

Leave a Reply

1 comment

  1. You can do that by using mini plugins on both FE and BE. The logic works like below;

    1. On backend, implement and action that triggered after post publish
    2. This triggered action will make a call to frontend service with specific username, password and post_id
    3. On FE, Implement another plugin that checks request that has username, password, and post_id
    4. If it has, instantianite W3_CacheFlush and call specific function.

    I have developed mini plugins, you can install them from admin panel. Simply, save both code as php file and zip it. Then upload to server. And also there are some important points on plugins. Serivce communication is made by username and password. So you need to give same username and password on both plugins. If not, they cannot communicate. I have put username and password to prevent another person to make call to your FE service. Here is the plugins.

    W3TC_BE.php

    <?php
    /*
    Plugin Name: W3TC Backend
    Plugin URI: http://huseyinbabal.net
    Description: Calls wstc cache clean on frontend services when new post published.
    Version: 1.0
    Author: Hüseyin BABAL
    Author URI: http://huseyinbabal.net
    */
    
    function call_cache_clean_service( $url, $post_id ) {
        if ( !function_exists( 'curl_version' ) ) {
            wp_die( 'Curl must be enabled' );
        }
    
        $url = 'http://frontendservice.com';
        $fields = array(
                        'username' => "your_username", // Username password protection for service usage. Those username password will be same on server side
                        'password' => "your_password",
                        'post_id' => $post_id
                        );
        foreach( $fields as $key => $value ) { 
            $fields_string .= $key . '=' . $value . '&'; 
        }
        rtrim($fields_string, '&');
    
        $ch = curl_init();
    
        curl_setopt($ch,CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_POST, count($fields));
        curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
    
        $result = curl_exec($ch);
        // To track which posts triggered
        error_log("[W3TC] Cache clean service called for : $post_id to $url", 3, '/path/to/log/file.txt');
        curl_close($ch);
    }
    
    // This for triggering service for all frontends
    function post_published( $post_id ) {
        $frontend_urls = array (
            "http://frontendservice1.com", 
            "http://frontendservice2.com", 
            "http://frontendservice3.com"
            );
        foreach ($frontend_urls as $url) {
            call_cache_clean_service( $post_id );
        }
    }
    
    // Publish action for calling service
    add_action( 'publish_post', 'post_published' );
    
    ?>
    

    W3TC_FE.php

    <?php
    /*
    Plugin Name: W3TC Frontend
    Plugin URI: http://huseyinbabal.net
    Description: Check specific request and clear cache
    Version: 1.0
    Author: Hüseyin BABAL
    Author URI: http://huseyinbabal.net
    */
    
    function check_w3c_request() {
        // this username and password must be same as in be
        $original_username = "your_username";
        $original_password = "your_password";
    
        $username = $_POST["username"];
        $password = $_POST["username"];
        $post_id = $_POST["post_id"];
    
        if ( (!empty($username) && $username != $original_username) || ( !empty($password) || $password != $original_password ) ) {
            wp_die( "Page not allowed!" );
        } else {
            if ( class_exists('W3_CacheFlush') ) {
                $w3_pgcache = w3_instance('W3_CacheFlush');
                return $w3_pgcache->prime_post( $post_id );     
            }   
        }
    
    
    }
    
    // Get Posted variables
    add_action( 'after_setup_theme', 'check_w3tc_request' );
    
    ?>