Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for modifying the cache content before sending to azure #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tsdexter
Copy link

We have a WordPress setup that requires us to modify the wordpress output buffer right before sending to the browser, this is accomplished with the below mu-plugin and a final_output filter in our theme - this happens too late for the PN Blob Cache to pick up and cache the changes:

ob_start();

add_action('shutdown', function () {
  $final = '';

  // We'll need to get the number of ob levels we're in, so that we can iterate over each, collecting
  // that buffer's output into the final output.
  $levels = ob_get_level();

  for ($i = 0; $i < $levels; $i++) {
    $final .= ob_get_clean();
  }

  // Apply any filters to the final output
  echo apply_filters('final_output', $final);
}, 0);

This PR adds support for users to modify the content that is sent to the Azure blob storage. It can be used by adding a filter on pn_cached_content to your theme or plugin. For example:

add_filter('pn_cached_content', function ($content, $url, $key) {
  // say you wanted to add the cache URL and/or key to your HTML for debugging purposes
  $modified_content = str_replace("<head>", "<head>\n<meta name=\"CACHEURL\" content=\"{$url}\">\n<meta name=\"CACHEKEY\" content=\"{$key}\">", $content);
  
  // or you had some other modification to the final output of wordpress
  $modified_content = YourThemesContentModificationFunction($content);

  return $modified_content;
}, 1, 3);

We are currently running this successfully in production on a site with ~1.5M pageviews/month.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant