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

Clear the Cache by Calling the Function Hook
Clear the Cache by Calling the Function Hook

You can use hooks to clear cache at specific points in the execution of the code. By strategically placing cache…

Placeholder Image
How to Remove the Footer Comment

By default, WP Fastest Cache adds an HTML comment in the page footer that indicates when and for how long…

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.

Ready to get started?

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