我可以有动态内容在一个WP页面缓存的WP超级缓存


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

尝试实现这里讨论的技术,

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

放到使用Genesis框架的站点中。我想知道是否有人使这种技术起作用,但更感兴趣的是是否有人使用Genesis使其起作用。

这里是我的代码,任何人感兴趣:

首先,页面模板文件(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();
?>

包含的文件将生成动态内容(test-content.php):

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

如有任何想法或见解,不胜感激。

PS-我可以关闭整个页面的缓存,这很容易。然而,我希望有缓存页面,其中某些部分仍然是动态生成的。我也可以在客户端通过javascript实现这一点,但这意味着重写我们编写的其他插件和小部件,这在这一点上不是一个选择。我还验证了所有WP超级缓存设置都是正确的技术(选择php缓存,后期初始化为真,动态缓存为真)。

将这一行移到wordpress的wp-config.php目录

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