WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Clear WP-SuperCache when WP-PostRating is made (8 posts)

  1. chadrew
    Member
    Posted 2 years ago #

    I'm trying to make WP PostRatings plugin clear WP Super Cache when a rating is made. I only want to clear the cache for the post which was rated.

    So I edited the process_ratings function in wp-postratings.php, and added wp_cache_post_change($post_id); to it. process_ratings is the function that checks if a user can rate, and then adds the custom fields and logs the results to the database, so I assumed that's where I should add this.

    Problem is, the cache is not cleared when a rating is made. I'm clueless about this stuff so it could be I'm missing something very obvious. The actual voting on the page uses AJAX, but I figured since process_ratings function is executed every time a rating is made, this should work...

    I tried checking if wp_cache_post_change function_exists and then echo something when a rating is made (works), and also modify some other custom fields when a rating is made (again, that worked). But not clearing the cache...

    Any thoughts?

  2. Donncha O Caoimh
    Member
    Posted 2 years ago #

    Have you tried the debug system in the plugin? That will tell you what is or not happening.

  3. chadrew
    Member
    Posted 2 years ago #

    Great tip. I don't know what to make of the results though. It seems cache only clears when I vote as a logged-in user.

    What happens in WPSC when I vote as anonymous user:

    10:53:14 /wp-content/plugins/wp-postratings/wp-postratings.php?pid=9&rate=1&_= supercache dir: /home/myuser/public_html/test/wp-content/cache/supercache/test.mydomain.com/wp-content/plugins/wp-postratings/wp-postratings.php/
    10:53:14 /wp-content/plugins/wp-postratings/wp-postratings.php?pid=9&rate=1&_= No wp-cache file exists. Must generate a new one.
    10:53:14 /wp-content/plugins/wp-postratings/wp-postratings.php?pid=9&rate=1&_= In WP Cache Phase 2
    10:53:14 /wp-content/plugins/wp-postratings/wp-postratings.php?pid=9&rate=1&_= Setting up WordPress actions
    10:53:14 /wp-content/plugins/wp-postratings/wp-postratings.php?pid=9&rate=1&_= Supercache caching disabled. Only using wp-cache. Non empty GET request.
    10:53:14 /wp-content/plugins/wp-postratings/wp-postratings.php?pid=9&rate=1&_= URI rejected. Not Caching

    I reload the page afterwards - old stuff is shown.

    What happens when I vote as logged-in admin ("Don’t cache pages for known users" is unchecked):

    10:56:05 /wp-content/plugins/wp-postratings/wp-postratings.php?pid=9&rate=5&_= Cookie detected: wordpress_logged_in_1b64b8eaccd745a3c91c4a1dc1905612
    10:56:05 /wp-content/plugins/wp-postratings/wp-postratings.php?pid=9&rate=5&_= supercache dir: /home/myuser/public_html/test/wp-content/cache/supercache/test.mydomain.com/wp-content/plugins/wp-postratings/wp-postratings.php/
    10:56:05 /wp-content/plugins/wp-postratings/wp-postratings.php?pid=9&rate=5&_= Cookie detected: wordpress_logged_in_1b64b8eaccd745a3c91c4a1dc1905612
    10:56:05 /wp-content/plugins/wp-postratings/wp-postratings.php?pid=9&rate=5&_= No wp-cache file exists. Must generate a new one.
    10:56:05 /wp-content/plugins/wp-postratings/wp-postratings.php?pid=9&rate=5&_= Cookie detected: wordpress_logged_in_1b64b8eaccd745a3c91c4a1dc1905612
    10:56:05 /wp-content/plugins/wp-postratings/wp-postratings.php?pid=9&rate=5&_= In WP Cache Phase 2
    10:56:05 /wp-content/plugins/wp-postratings/wp-postratings.php?pid=9&rate=5&_= Setting up WordPress actions
    10:56:05 /wp-content/plugins/wp-postratings/wp-postratings.php?pid=9&rate=5&_= Supercache caching disabled. Only using wp-cache. Non empty GET request.
    10:56:05 /wp-content/plugins/wp-postratings/wp-postratings.php?pid=9&rate=5&_= URI rejected. Not Caching
    10:56:05 /wp-content/plugins/wp-postratings/wp-postratings.php?pid=9&rate=5&_= Post change: deleting post cache files for test.mydomain.com/9/another-game-review: /home/myuser/public_html/test/wp-content/cache/wp-cache-27074f6ef064dd48f2d0c47a769458f3.html

    The last line - cache is actually getting cleared as I wanted.

    (Using PHP mode)

  4. Donncha O Caoimh
    Member
    Posted 2 years ago #

    I don't know why that happens. The first log shows the postratings file wasn't cached. It should have shown the clear cache command running too.

  5. chadrew
    Member
    Posted 2 years ago #

    I think I got something from looking at the debug logs.

    If admin visits the post and "Don’t cache pages for known users" is not checked, only WP-Cache file is created (no supercache).

    If anonymous user visits the post, only supercache file is created (no wp-cache).

    wp_cache_post_change($post_id); actually seems to clear only wp-cache (not supercache). I'm saying this because nothing happens when I vote as anonymous user - unless admin has visited the post (and created wp-cache as a result) before.

    So if I visit the said post as an admin (with "Don’t cache pages for known users" not checked), a WP-Cache file is created; then I visit as an anonymous user and vote, and wp-cache is cleared as it should!

    11:59:49 /wp-content/plugins/wp-postratings/wp-postratings.php?pid=20&rate=5 Post change: deleting post cache files for test.mydomain.com/20/a-really-awesome-post: /home/myuser/public_html/test/wp-content/cache/wp-cache-a812edcce01ae89bc087f591d4ab1705.html
    11:59:51 /20/a-really-awesome-post supercache dir: /home/myuser/public_html/test/wp-content/cache/supercache/test.mydomain.com/20/a-really-awesome-post/
    11:59:51 /20/a-really-awesome-post Served page from supercache file using PHP.

    However, an old supercache file is still served even after wp_cache_post_change($post_id); was used, so anonymous user still sees old results after refreshing the page.

  6. chadrew
    Member
    Posted 2 years ago #

    I've looked at other plugins which might clear Super Cache, and found out that I have to "tell" it that super cache is enabled with $GLOBALS["super_cache_enabled"]=1;. So this works:

    if (function_exists ('wp_cache_post_change')) {
    $GLOBALS["super_cache_enabled"]=1;
    wp_cache_post_change($post_id);
    }
  7. Donncha O Caoimh
    Member
    Posted 2 years ago #

    Ah, good catch. I should add that as a FAQ or to the developer docs!

  8. globalistas
    Member
    Posted 2 years ago #

    Hi guys, I'm using WTI Like Post plugin (basically a thumbs up/down voting plugin) and have the same issue as the OP. I'm not sure what exactly do I need to do to NOT cache the voting results.

    Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic