【问题标题】:Can I have dynamic content in a WP page cached by WP Super Cache?我可以在 WP Super Cache 缓存的 WP 页面中包含动态内容吗?
【发布时间】:2015-10-23 05:26:34
【问题描述】:

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

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 Super Cache 设置对于该技术都是正确的(选择了 php 缓存,延迟初始化为真,动态缓存为真)。

【问题讨论】:

    标签: php wordpress caching


    【解决方案1】:

    将此行移至您的 wordpress 的 wp-config.php。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-24
      • 1970-01-01
      • 2018-04-14
      • 2017-06-18
      • 2016-11-11
      相关资源
      最近更新 更多