There is a fine tutorial over on WP-Rocket’s blog on how to implement WP Rocket with a Bedrock/Trellis setup. You can find this tutorial here https://wp-rocket.me/blog/how-to-set-up-wp-rocket-on-trellis-and-bedrock/

However, there are a few missing things in this tutorial. I figured out that caching didn’t work as expected after deploying and sometimes didn’t work at all. Here, I will try to fix these issues.

First, go ahead and follow the above tutorial. Then continue with the below steps:

Recreate advanced-cache.php and clear cache on deploy

WP Rocket saves its settings in a file named advanced-cache.php. It is automatically regenerated each time you change your WP Rocket settings.

You need to regenerate this file on each deploy as WP Rocket finds the version path. Not the current path, so configuration lines in this file will look like this:

$rocket_path = '/srv/www/your-domain.com/releases/20200507181234/web/app/plugins/wp-rocket/';

This is of course not very good (as 20200507181234 will change for each deploy).  So we need to update this file each time we deploy in order to update to the new release path.

Furthermore, we need to clear the cache upon deploy as all links to generated assets like stylesheets and JavaScripts will be updated and the contents of WP Rocket’s cached files are invalid. Here is how to do this:

In the folder site/web create a file named wp-rocket-cache-helper.php  and insert this content into the file:

<?php
// Load WordPress.
require( 'wp/wp-load.php' );

// Regenerate advanced-cache.php
if ( function_exists( 'rocket_generate_advanced_cache_file' ) ) {
	rocket_generate_advanced_cache_file();
}

// Clear cache.
if ( function_exists( 'rocket_clean_domain' ) ) {
	rocket_clean_domain();
}

This will first regenerate the advanced-cache.php  file and after that clear the cache

Now, we need to make this file execute on deploy.

Go to trellis/deploy-hooks, create a file named finalize-after.yml, and insert the following:

# WP Rocket settings below
- name: WP Rocket cache helper for Trellis/Bedrock
  command: php wp-rocket-cache-helper.php
  args:
    chdir: "{{ deploy_helper.new_release_path }}/web"

This will use the command line PHP in the server you are deploying to, to call the functions in the wp-rocket-cache-helper.php file.

Now, we need to make sure that finalize-after.yml is executed when we deploy. In order to do so navigate to the folder trellis/roles/deploy/defaults and open the file main.yml

Here, you should find the line that says deploy_finalize_after  and alter it so it looks like this:

deploy_finalize_after:
  - "{{ playbook_dir }}/roles/deploy/hooks/finalize-after.yml"
  # WP Rocket settings below
  - "{{ playbook_dir }}/deploy-hooks/finalize-after.yml"

You should now commit and push your changes to your repository and deploy your changes as you would do normally.

Take it further

You can take it even further. Say you want to start preloading automatically after deploying, you just add that function to wp-rocket-cache-helper.php. See more in this article: https://docs.wp-rocket.me/article/494-how-to-clear-cache-via-cron-job

Leave a Reply

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