[PATH] Clear cache button
-
I noticed there’s no clear cache button. This can be an issue for example when you change the date or add new posts that should relate to a previous one.
As an example if you have Post X that has a Post Y in its related post list then change the date on Post Y, the URL on the cached post list on the Post X page won’t update accordingly and it’ll lead to a 404.
Another example is if you create Post X and it has no related posts. If you then create Post Y and want it to relate to X, you probably want Y to appear in X’s related post list. A clear cache button is required to do this so next page load a new related post list is generated.I added a simple ‘Clear Cache’ button to the admin like so. In admin.inc.php below
<p class="description"><?php _e('Enabling this option will cache the related posts output when the post is visited the first time. The cache is cleaned when you save this page.',CRP_LOCAL_NAME); ?></p>add
<p><input type="button" value="<?php _e('Clear cache',CRP_LOCAL_NAME) ?>" onclick="return clearCache();" /></p>and above
function checkForm() {in the JS at the bottom of the file add
function clearCache() { // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php jQuery.post(ajaxurl, {action: 'crp_clear_cache'}, function(response, textStatus, jqXHR) { alert( response.message ); }, 'json'); }
That’s the HTML taken care of, now for the actual logic to delete the cache. In contextual-related-posts.php at the bottom above// End admin.incadd
add_action('wp_ajax_crp_clear_cache', 'crp_ajax_clearcache'); function crp_ajax_clearcache() { global $wpdb; // this is how you get access to the database $rows = $wpdb->query(" DELETE FROM " . $wpdb->postmeta . " WHERE meta_key='crp_related_posts' "); // Did an error occur? if ( $rows === false ) exit(json_encode(array( 'success' => 0, 'message' => "An error occurred clearing the cache. Please contact your site administrator.\n\nError message:\n" . $wpdb->print_error(), ))); // No error, return the number of else exit(json_encode(array( 'success' => 1, 'message' => $rows . " cached row(s) cleared.", ))); }
While I was in there I also fixed an error you get when updating settings with WP_DEBUG turned on:Notice: Undefined index: post_types in /path/to/wp-content/plugins/contextual-related-posts/admin.inc.php on line 84
by changing
$post_types_arr = (is_array($_POST['post_types'])) ? $_POST['post_types'] : array('post' => 'post');to
$post_types_arr = (isset($_POST['post_types']) && is_array($_POST['post_types'])) ? $_POST['post_types'] : array('post' => 'post');
The topic ‘[PATH] Clear cache button’ is closed to new replies.