• Resolved christopheran

    (@christopheran)


    Ok, I’m trying to work out having some dynamic content with the rest of the page cached.

    I’m looking at the examples from:

    https://z9.io/2013/10/21/shiny-new-dynamic-content-wp-super-cache/
    https://svn.wp-plugins.org/wp-super-cache/trunk/plugins/dynamic-cache-test.php

    I’m using the first example script exactly as it is on the example page, with the exception of adding ‘testtest’ as the tag. (did I do that correctly?)

    Here’s what I’ve got

    [ Moderator note: code fixed. Please wrap code in the backtick character or use the code button. ]

    -----------------------------
    
    define( 'DYNAMIC_CACHE_TEST_TAG', 'testtest' ); // Change this to a secret placeholder tag
    if ( DYNAMIC_CACHE_TEST_TAG != '' ) {
    	function dynamic_cache_test_safety( $safety ) {
    		return 1;
    	}
    	add_cacheaction( 'wpsc_cachedata_safety', 'dynamic_output_buffer_test_safety' );
    
    	function dynamic_cache_test_filter( &$cachedata) {
    		return str_replace( DYNAMIC_CACHE_TEST_TAG, "<!-- Hello world at " . date( 'H:i:s' ) . " -->", $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' );
    }
    
    -----------------------------------

    I pasted it into single.php (inside php tags) just after my post content is generated. I clear the cache, view the source code… but nothing shows up from this script.

    In the instructions it says I need to include the tag in the theme.

    So I add (outside of php tags) “testtest” under the script.

    At that point I should get the “<!– Hello world at…” in the source code, correct?

    I don’t get anything but “testtest” there.

    I tried putting the script into my functions.php and that didn’t make a difference. I wasn’t sure where it was supposed to go, on single.php or functions.php (or somewhere else?)

    I deleted the cache each time I tested.

    I tried doing this:

    ---------
    if ( function_exists( 'dynamic_cache_test_filter' ) ) {
        dynamic_output_buffer_test();
    ?>testtest<?php }
    ---------

    and the ‘testtest’ disappeared but was not replaced. Does that mean the script isn’t working?

    On my WP Super Cache advanced settings, I have ‘Use PHP’, ‘Enable Dynamic Cacheing’, and ‘Late Init’ enabled.

    Where am I going wrong?

    Thanks for any help!

    Chris

    https://www.ads-software.com/plugins/wp-super-cache/

Viewing 6 replies - 16 through 21 (of 21 total)
  • Ok,

    I figured out how to make the dynamic cache work, using the simple replace method, but I can’t use the output buffer.

    This is the code I put inside functions.php file of my theme

    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
    	strtest();
    	$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 );
    }
    function dynamic_output_buffer_init() {
    	add_action( 'wp_footer', 'dynamic_output_buffer_test' );
    }
    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.
    }
    function strtest() {
      echo "hello";
    }

    And this is what I put in my template

    add_cacheaction( 'wpsc_cachedata', 'dynamic_output_buffer_test' );
     add_cacheaction( 'add_cacheaction', 'dynamic_output_buffer_init' );
     add_cacheaction( 'wpsc_cachedata_safety', 'dynamic_output_buffer_test_safety' );
        ?>MY_BUFFER_TAG

    What am I doing wrong here?

    Ok, that didn’t work because there was no call to dynamic_output_buffer_test function in my template.

    So, the correct code should be

    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
    	strtest();
    	$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 );
    }
    function dynamic_output_buffer_init() {
    	add_action( 'wp_footer', 'dynamic_output_buffer_test' );
    }
    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.
    }
    function strtest() {
      echo "hello";
    }
    
    add_cacheaction( 'wpsc_cachedata', 'dynamic_output_buffer_test' );
    add_cacheaction( 'add_cacheaction', 'dynamic_output_buffer_init' );
    add_cacheaction( 'wpsc_cachedata_safety', 'dynamic_output_buffer_test_safety' );

    to use in functions.php, and

    if ( function_exists( 'dynamic_output_buffer_test' ) )
          strtest();
        ?>MY_BUFFER_TAG

    to use in my theme page.

    Now, what if I need more than one section to stay dynamic? I would need to use more than a buffer, but how?
    I adjusted the code, in order to output the different dynamic parts, when the function is called from my theme, but I get the same result in all positions. For example, if I want a mini cart widget and another sidebar to stay dynamic, I get two mini carts, the first in its own place, and the second replacing the other sidebar.

    Hey christopheran!
    Can you please tell my step by step to get dynamic content to work? I’m struggling more that week to get this work, but I just can’t get it to work. There is no more info to set it up. I would really appreciate if you could give instructions.

    Thread Starter christopheran

    (@christopheran)

    Hi 0utf1t,

    I’m sorry, I gave up on the dynamic feature of this. Well, not really gave up. I had it working, but found that the dynamic content still generated to much pull on my server so I changed my site so that the dynamic content was not needed on page.

    It’s been so long since I delved into all this, I really don’t remember all the details.

    Chris

    Hello stebcom,

    Can you please explain your use of the dynamic function?

    It appears you’re simply calling a function that prints Hello and doesn’t actually replace the ‘MY_BUFFER_TAG string that is supposed to be replaced?

    Can anyone provide a fool-proof guide to get a simple widget displayed as a dynamic area?

    I don’t quite understand how to use the functions in the tutorial.

    Regards,

    J

    Ok, thank you for answer.

    Can I ask you some question, maybe you remember something you did?

    Right now I’ve enabled php mode, dynamic cache and late init. I changed secret placeholder tag and upload it back in wp super cache plugins folder. I see hello world with time in my source code. But I can’t get dynamic content to appear where I want it to be. So if you remember I would really appreciate if you can answer following questions:

    1) Did you write unique placeholder tag in dynamic-cache-test.php and upload it back into super cache plugins folder?
    2) Where did you put that long script (in your 1.post) in single.php and functions.php?
    3) Did you put the whole script in php tags ==> <?php ?> ?
    4) This ==>
    if ( function_exists( ‘dynamic_cache_test_filter’ ) ) {
    dynamic_output_buffer_test();
    ?>testtest<?php }
    ==< I put in the theme where I want dynamic content to appear? But it’s incorrect, right?

Viewing 6 replies - 16 through 21 (of 21 total)
  • The topic ‘Trying to understand dynamic cacheaction filter…’ is closed to new replies.