Inicio - Documentación - POM Cache - 09 Developers - Filter rendered output

Filter rendered output

POM Cache offers two output filters with different audiences. Choose the filter according to whether the current visitor should see the modification or only future cache hits should see it.

Change the live response with pom_cache_ob_callback_filter

This filter runs on the rendered buffer before the final eligibility checks:

add_filter(
    'pom_cache_ob_callback_filter',
    static function ( $buffer ) {
        return str_replace(
            '<meta name="generator" content="Example">',
            '',
            (string) $buffer
        );
    }
);

The returned string is:

  • sent to the current visitor;
  • evaluated for response completeness and eligibility;
  • used as the candidate content if POM Cache writes a file.

Do not use this hook merely to decide whether to cache. Use DONOTCACHEPAGE or an eligibility mechanism for that purpose. Always return a string, and avoid expensive document-wide parsing on every dynamic page render.

Change only the stored copy with pom_cache_buffer

pom_cache_buffer runs after POM Cache has decided to write and has added its generation-time comment:

add_filter(
    'pom_cache_buffer',
    static function ( $cached_copy ) {
        return str_replace(
            '<!-- public-cache-marker -->',
            '<span class="cache-label">Cached public response</span>',
            (string) $cached_copy
        );
    }
);

The current visitor still receives the original render. Later cache hits receive the filtered copy. POM Cache appends its final cache comments after this filter.

This difference can make diagnosis confusing. Use stored-copy transformations only when the divergence is intentional, deterministic, and documented.

Output that must never be introduced

Neither hook is safe for:

  • user names or account data;
  • carts, sessions, or authorization results;
  • nonces or one-time tokens;
  • request-specific secrets;
  • random content;
  • timestamps that should update on every request;
  • geolocation or experiments without an independent safe cache key.

The output must be identical for every visitor represented by the URL's cache file.

Preserve a valid document

POM Cache normally requires a recognized completion marker before writing. A transformation that removes a closing tag or creates malformed output can prevent generation or store unusable content.

When changing the buffer:

  1. test the uncached response;
  2. inspect the generated file through a subsequent hit;
  3. validate markup and response encoding;
  4. compare headers and body for anonymous and logged-in requests;
  5. confirm that invalidation regenerates the transformed result.

Avoid accidental recursion or side effects

Keep callbacks pure: transform the provided string and return it. Do not make remote requests, save posts, clear caches, or trigger another front-end render inside an output filter.

If the transformation comes from a deploy-time asset or configuration value, clear the HTML cache after that value changes. POM Cache cannot infer dependencies introduced by third-party buffer callbacks.