简体   繁体   中英

Can I have dynamic content in a WP page cached by WP Super Cache?

Trying to implement the technique discussed here,

http://z9.io/2013/10/21/shiny-new-dynamic-content-wp-super-cache/

into a site that is using the Genesis framework. I was wondering if anybody has made this technique work at all, but more so interested if anybody has made it work using Genesis.

Here is my code for anybody interested:

First, the page template file (test-age.php):

<?php

/*
 * Template Name: Test Template
 */

remove_action( 'genesis_loop', 'genesis_do_loop' );
remove_action( 'genesis_entry_content', 'genesis_do_post_content' );
add_action( 'genesis_loop', 'support_loop' );

// Content Area

define( 'DYNAMIC_OUTPUT_BUFFER_TAG', 'moonshine' ); // 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
        include( 'test-content.php' );
        $text = ob_get_contents();
        ob_end_clean();
        if ( $cachedata === 0 ) { // 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' );
}

function support_loop() {
    echo "\n"; // make source views more pleasant for debugging
    ?>
    <div class="test-page">
        <?php
        if ( function_exists( 'dynamic_output_buffer_test' ) )
            dynamic_output_buffer_test();
        ?>moonshine<?php
    ?>
    </div>
    <?php
    echo "\n"; // make source views more pleasant for debugging
}

genesis();
?>

And the file that contains the would produce the dynamic content (test-content.php):

<?php
echo "The test was successful!!!!";
?>

Any thoughts or insights would be appreciated.

PS- I can turn caching off for an entire page, that is easy. However, I looking to have cached pages where certain sections are still dynamically generated. I could also do this via javascript on the client-side, but that would mean rewriting other plugins and widgets we have written and that is not an option at this point. I have also verified that all WP Super Cache settings are correct for the technique (php caching selected, late init true, dynamic caching true).

Move this line to the wp-config.php of your wordpress.

// Change this to a secret placeholder tag
define( 'DYNAMIC_OUTPUT_BUFFER_TAG', 'moonshine' ); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM