WordPress.org

Ready to get started?Download WordPress

Forums

WP Super Cache
[resolved] Trying to understand dynamic cacheaction filter... (18 posts)

  1. christopheran
    Member
    Posted 1 month ago #

    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:

    http://z9.io/2013/10/21/shiny-new-dynamic-content-wp-super-cache/
    http://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://wordpress.org/plugins/wp-super-cache/

  2. christopheran
    Member
    Posted 1 month ago #

    To mod: Thank you for the 'code' correction. Will do.

    If this helps, here is my debug log:

    14:17:03 /rick-is-back/36710855/ Supercache Late Init: add wp_cache_serve_cache_file to init
    14:17:03 /rick-is-back/36710855/ Supercache Late Loader running on init
    14:17:03 /rick-is-back/36710855/ supercache dir: /home/nfstest/public_html/wp-content/cache/supercache/test.mysite.com/rick-is-back/36710855/
    14:17:03 /rick-is-back/36710855/ Fetched dynamic page data from supercache file using PHP. File: /home/nfstest/public_html/wp-content/cache/supercache/test.mysite.com/rick-is-back/36710855/index.html
    14:17:03 /wp-content/plugins/top-10/top-10-addcount.js.php?top_ten_id=10855 Supercache Late Init: add wp_cache_serve_cache_file to init
    14:17:03 /wp-content/plugins/top-10/top-10-addcount.js.php?top_ten_id=10855 Supercache Late Loader running on init
    14:17:03 /wp-content/plugins/top-10/top-10-addcount.js.php?top_ten_id=10855 supercache dir: /home/nfstest/public_html/wp-content/cache/supercache/test.mysite.com/wp-content/plugins/top-10/top-10-addcount.js.php/
    14:17:03 /wp-content/plugins/top-10/top-10-addcount.js.php?top_ten_id=10855 No Super Cache file found for current URL: /home/nfstest/public_html/wp-content/cache/supercache/test.mysite.com/wp-content/plugins/top-10/top-10-addcount.js.php/index.html
    14:17:03 /wp-content/plugins/top-10/top-10-addcount.js.php?top_ten_id=10855 In WP Cache Phase 2
    14:17:03 /wp-content/plugins/top-10/top-10-addcount.js.php?top_ten_id=10855 Setting up WordPress actions
    14:17:03 /wp-content/plugins/top-10/top-10-addcount.js.php?top_ten_id=10855 Supercache caching disabled. Only using wp-cache. Non empty GET request. Array
    (
        [top_ten_id] => 10855
    )
    
    14:17:03 /wp-content/plugins/top-10/top-10-addcount.js.php?top_ten_id=10855 Created output buffer
    14:17:03 /wp-content/plugins/top-10/top-10-addcount.js.php?top_ten_id=10855 URI rejected. Not Caching
    14:17:03 /wp-content/plugins/top-10/top-10-addcount.js.php?top_ten_id=10855 wp_cache_maybe_dynamic: returned $buffer
    14:17:05 /wp-admin/admin-ajax.php Supercache Late Init: add wp_cache_serve_cache_file to init
    14:17:05 /wp-admin/admin-ajax.php Supercache Late Init: add wp_cache_serve_cache_file to init
    14:17:05 /wp-admin/admin-ajax.php Supercache Late Loader running on init
    14:17:05 /wp-admin/admin-ajax.php supercache dir: /home/nfstest/public_html/wp-content/cache/supercache/test.mysite.com/
    14:17:05 /wp-admin/admin-ajax.php No Super Cache file found for current URL: /home/nfstest/public_html/wp-content/cache/supercache/test.mysite.com/index.html
    14:17:05 /wp-admin/admin-ajax.php In WP Cache Phase 2
    14:17:05 /wp-admin/admin-ajax.php Setting up WordPress actions
    14:17:05 /wp-admin/admin-ajax.php Not caching wp-admin requests.
    14:17:05 /wp-admin/admin-ajax.php Supercache Late Loader running on init
    14:17:05 /wp-admin/admin-ajax.php supercache dir: /home/nfstest/public_html/wp-content/cache/supercache/test.mysite.com/
    14:17:05 /wp-admin/admin-ajax.php No Super Cache file found for current URL: /home/nfstest/public_html/wp-content/cache/supercache/test.mysite.com/index.html
    14:17:05 /wp-admin/admin-ajax.php In WP Cache Phase 2
    14:17:05 /wp-admin/admin-ajax.php Setting up WordPress actions
    14:17:05 /wp-admin/admin-ajax.php Not caching wp-admin requests.
  3. Donncha O Caoimh
    Member
    Plugin Author

    Posted 1 month ago #

    There's a bug in the example script that I've fixed in trunk. (You'll see the correct version in the second link you posted above. The line
    add_cacheaction( 'wpsc_cachedata_safety', 'dynamic_output_buffer_test_safety' );

    Now reads
    add_cacheaction( 'wpsc_cachedata_safety', 'dynamic_cache_test_safety' );

    Check your PHP error log, that's probably where things went wrong.

  4. christopheran
    Member
    Posted 1 month ago #

    I changed that (and emptied cache) and retried but still get the same result....

  5. christopheran
    Member
    Posted 1 month ago #

    Set up php error logging and I'm not getting any errors on this.

  6. christopheran
    Member
    Posted 1 month ago #

    ........ this took me a crazy number of hours to figure this out. Really - I'd be embarrassed to tell you how long I've been trying to work this out.

    But I finally did.

    I had to change the name of DYNAMIC_CACHE_TEST_TAG throughout the script. Not the assigned string, the actual "DYNAMIC_CACHE_TEST_TAG". For example, if I go through the script a change DYNAMIC_CACHE_TEST_TAG to DYNAMIC_CACHE_TESTTAG, then it works.

    I don't know why, do you?

    Ok, now that that's working... I'm going to bed. I may have more questions for you when I actually go to put this into practice :)

    Chris

  7. Donncha O Caoimh
    Member
    Plugin Author

    Posted 1 month ago #

    I think I know why. DYNAMIC_CACHE_TEST_TAG is defined in the example script in wp-content/plugins/wp-super-cache/plugins/ but you should have received a PHP error about that.

  8. christopheran
    Member
    Posted 1 month ago #

    opps, may have spoke to soon - I was getting a bit bleary eyed last night :)

    It only seems to be working (replacing the string) on the first page load. After it is cached, I just get the string on the page. I can delete the cache and reload the page and it works again that one time. Then on subsequent page loads I'm back to the string...

    Chris

  9. christopheran
    Member
    Posted 1 month ago #

    Ok, I deleted dynamic-cache-test.php from the plugins folder and now the example script is working. So I guess one of the functions was getting confused by that as well?

  10. christopheran
    Member
    Posted 1 month ago #

    Darn it, I was wrong with that last post. I was logged in when I deleted that file and it seemed to work.

    Testing with a browser that is not logged into wp, the string is transformed on the first load. On loads after that, it just displays the string.

  11. christopheran
    Member
    Posted 1 month ago #

    Came back to test this fresh today.

    /plugins/dynamic-cache-test.php is deleted, so that is not a factor.

    Used the example exactly as is, no changes.

    On the first load, the secret string is replaced.

    All loads after that (the cached loads) just show the secret string (when not logged in to wordpress). The replacement doesn't occur.

    Donncha, does the example script work for you when you test it?

    Chris

  12. christopheran
    Member
    Posted 1 month ago #

    opps, repeat was here, I don't see a way to delete posts - sorry.

  13. christopheran
    Member
    Posted 1 month ago #

    Ah, ok...

    I mentioned above that I put the example script into my single.php. I learned about all this from http://z9.io/2013/10/21/shiny-new-dynamic-content-wp-super-cache/ and the example script there.

    I did not realize it was designed as a plugin to wp super cache and had to be in the plugins folder to work.

    I kept experimenting and tried moving it to the plugins folder and NOW it works :)

    You might mention in the tutorial where it needs to go to be used. I might not be the only one this happens to,

    Chris

  14. christopheran
    Member
    Posted 1 month ago #

    Working now... but whatever is dynamic is also showing up in the footer at the very bottom of the page.

    Happens whether I have the string included or not. Tried changing the string to a different set up letters and changing themes. Same results.

    I have "Hello world at 05:47:33" (with updated time) at the bottom of every post - and also where I put it using the string.

    Any thoughts on this?

  15. christopheran
    Member
    Posted 1 month ago #

    Ok, resolved this by removing:

    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' );
  16. stebcom
    Member
    Posted 2 weeks ago #

    Hi,
    I'm trying to implement the dynamic cache too, but I'm quite lost by now.
    I got the dynamic-cache-test.php script, moved into a dynamic-cache-test folder under plugins folder and activated it as a new plugin.
    I defined DYNAMIC_CACHE_TEST_TAG, then I edited a page from my theme, switching this code:

    <?php woocommerce_mini_cart(); ?>

    with this:

    <!--mydynamicdata--><br />
        <?php woocommerce_mini_cart(); ?><br />
        <!--/mydynamicdata-->

    I expected to see the Hello World text replacing the cart, but I just see the string

    -->

    before and after the cart widget content.

    If I just put my secret string in a template page, that string is removed, but replaced with nothing.

    Can you point me to the correct using of the dynamic cache functionality?

  17. stebcom
    Member
    Posted 1 week ago #

    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?

  18. stebcom
    Member
    Posted 1 week ago #

    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.

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.