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
How to Serve Cache Only via PHP

On servers using apache, the cache is served via htaccess thanks to the rewrite rule. It is the best method…

How to Enable Gzip Compression in WordPress
How to Enable Gzip Compression in WordPress

Used by more than half of all websites, Gzip Compression remains popular today.

Placeholder Image
htaccess Gzip doesn’t work

Sometimes the files are not compressed although the gzip option has already been enabled.

Ready to get started?

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