CARVIEW |
Select Language
HTTP/2 200
server: nginx
date: Sat, 11 Oct 2025 20:34:43 GMT
content-type: text/plain
vary: Accept-Encoding
last-modified: Mon, 23 Jan 2023 06:59:36 GMT
etag: W/"2852729//wp-super-cache/trunk/plugins/dynamic-cache-test.php"
cache-control: max-age=604800
alt-svc: h3=":443"; ma=86400
content-encoding: gzip
', $cachedata );
}
add_cacheaction( 'wpsc_cachedata', 'dynamic_cache_test_filter' );
function dynamic_cache_test_template_tag() {
echo DYNAMIC_CACHE_TEST_TAG; // This is the template tag.
}
function dynamic_cache_test_init() {
add_action( 'wp_footer', 'dynamic_cache_test_template_tag' );
}
add_cacheaction( 'add_cacheaction', 'dynamic_cache_test_init' );
}
/*
* EXAMPLE 2
*
* This is going to be complicated. Hang on!
*
* When the cache file for a new page is generated the plugin uses an output
* buffer to capture the page. A callback function processes the buffer and
* writes to the cache file. The placeholder tag for any dynamic content has
* to be written to that cache file but also, it has to be replaced with
* dynamic content before the page is shown to the user.
* More on output buffers here: https://php.net/ob_start
*
* Unfortunately an extra output buffer is often required when capturing dynamic
* content such as sidebar widgets. Due to a quirk of the way PHP works it's
* not possible to have an output buffer run in an output buffer callback. That
* dynamic content has to be generated before the callback function is reached.
* The following error occurs when an output buffer is created in the
* callback function of another output buffer:
* "PHP Fatal error: ob_start(): Cannot use output buffering in output buffering display handlers in..."
*
* In this example the function add_action() isn't available when this file is
* loaded so dynamic_output_buffer_init() is hooked on to the "add_cacheaction"
* cacheaction. That function then hooks dynamic_output_buffer_test() on to the
* familiar wp_footer action.
*
* The first time dynamic_output_buffer_test() runs it generates the dynamic
* content and captures it with ob_start() in the DYNAMIC_OB_TEXT constant.
*
* When the main WP Super Cache output buffer is ready the callback is called.
* This fires the wpsc_cachedata_safety filter. If the DYNAMIC_OB_TEXT constant
* is set, which means dynamic content is ready, then it returns 1, a signal
* that everything is ok.
* Finally, the wpsc_cachedata filter is run. The function
* dynamic_output_buffer_test() is hooked on to it. Since DYNAMIC_OB_TEXT is
* set it replaces the placeholder text with that constant.
* The resulting html is then sent to the browser.
*
* Already cached pages call the safety filter, and then the wpsc_cachedata
* filter so any hooked function must be ready to generate dynamic content. The
* very last line of dynamic_output_buffer_test() replaces the placeholder tag
* with the dynamic content in the cache file.
*
* Use an output buffer to capture dynamic content while the page is generated
* and insert into the right place:
* Remember to add the DYNAMIC_OUTPUT_BUFFER_TAG text (as defined below) to
* your theme where the dynamic content should be.
*
* dynamic_output_buffer_test() is a function that uses the wpsc_cachedata
* filter to add a small message and the current server time to every web
* page. The time increments on every reload.
*
*/
define( 'DYNAMIC_OUTPUT_BUFFER_TAG', '' ); // Change this to a secret placeholder tag.
if ( '' !== DYNAMIC_OUTPUT_BUFFER_TAG ) {
function dynamic_output_buffer_test( $cachedata = 0 ) {
if ( defined( 'DYNAMIC_OB_TEXT' ) ) {
return str_replace( DYNAMIC_OUTPUT_BUFFER_TAG, DYNAMIC_OB_TEXT, $cachedata );
}
ob_start();
// call the sidebar function, do something dynamic
echo '
This is a test. The current time on the server is: ' . date( 'H:i:s' ) . '
'; $text = ob_get_contents(); ob_end_clean(); if ( 0 === $cachedata ) { // called directly from the theme so store the output. define( 'DYNAMIC_OB_TEXT', $text ); } else { // called via the wpsc_cachedata filter. We only get here in cached pages in wp-cache-phase1.php. return str_replace( DYNAMIC_OUTPUT_BUFFER_TAG, $text, $cachedata ); } } add_cacheaction( 'wpsc_cachedata', 'dynamic_output_buffer_test' ); function dynamic_output_buffer_init() { add_action( 'wp_footer', 'dynamic_output_buffer_test' ); } add_cacheaction( 'add_cacheaction', 'dynamic_output_buffer_init' ); function dynamic_output_buffer_test_safety( $safety ) { if ( defined( 'DYNAMIC_OB_TEXT' ) ) {// this is set when you call dynamic_output_buffer_test() from the theme. return 1; // ready to replace tag with dynamic content. } else { return 0; // tag cannot be replaced. } } add_cacheaction( 'wpsc_cachedata_safety', 'dynamic_output_buffer_test_safety' ); }