VOOZH about

URL: https://www.wpfastestcache.com/tutorial/buffer-callback-filter/

⇱ Buffer Callback Filter


WP Fastest Cache
Blog Features Pricing FAQ Testimonials

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 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 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;
}
Sign Up for Exclusive Discount Emails!
Get our exclusive discount campaign news delivered straight to your mailbox.

Related articles

👁 Placeholder Image

Tutorial

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…

👁 Placeholder Image

Tutorial

Executing a Function After Clearing All Cache

You can write your own function that will work after the cache is cleared.

👁 Create a Post Cache by ID Using the Function Hook

Tutorial

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.

Sign Up Now