Buffer Callback Filter

by admin

You can use the Buffer Callback Filter feature to make any changes to the html source of a page or minified css/js files before saving.

Parameters

$buffer: (string) Output in the buffer
$extension: (string) The type of the buffer (html, cache, css or js)
$cache_file_path: (string) The file path of buffer

Usage

add_filter( 'wpfc_buffer_callback_filter', 'your_function_name', 10, 3);

function your_function_name($buffer, $extension, $cache_file_path){
    // Your code
    return $buffer;
}

We think it would be more useful to explain it with an example.

Examples

1. How to Exclude a Page from Cache

In this scenario, if the html source of a page contains the keyword “specified_keyword”, the page is not cached.

If the extension is set as “html”, the filter acts on content before WP Fastest Cache has manipulated it.

add_filter( 'wpfc_buffer_callback_filter', 'your_function_name', 10, 3);

function your_function_name($buffer, $extension, $cache_file_path){
    if($extension == "html"){
        if(preg_match("/specified_keyword/", $buffer)){
            return false;
        }
    }

    return $buffer;
}

2. How to Fix Trailing Slash on Void Elements

In this scenario, We’re resolving the “Trailing slash on void elements” warning, which is one of the notifications you get in The W3C Markup Validation Service.

If the extension is set as “cache”, the filter acts on content after WP Fastest Cache has manipulated it.

add_filter('wpfc_buffer_callback_filter', 'remove_trailing_slash_on_void_elements', 10, 3);

function remove_trailing_slash_on_void_elements($buffer, $extension, $cache_file_path){
    if($extension == "cache"){
        return preg_replace("/<(meta|link|input)\s+((?:(?!\s*\/\s*>).)+)\s*\/\s*>/", "<$1 $2>", $buffer);
    }
    
    return $buffer;
}

Related articles

Placeholder Image
Modify Minified CSS by Calling the Function Hook

You can write your own function that will modify the content of minified or combined css sources before saving.

Create a Post Cache by ID Using the Function Hook
Create a Post Cache by ID Using the Function Hook

This function is designed for manually creating page caches.

Placeholder Image
Exclude Urls by Calling the Function Hook

It is very simple to exclude any url from the cache. You can call the following function in a theme…

Ready to get started?

Purchase your first license and see why 1,500,000+ websites globally around the world trust us.