You can do that by using mini plugins on both FE and BE. The logic works like below;
- On backend, implement and action that triggered after post publish
- This triggered action will make a call to frontend service with specific
username
,password
andpost_id
- On FE, Implement another plugin that checks request that has
username
,password
, andpost_id
- 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' );
?>