Title: [PATH] Clear cache button
Last modified: August 21, 2016

---

# [PATH] Clear cache button

 *  [flynsarmy](https://wordpress.org/support/users/flynsarmy/)
 * (@flynsarmy)
 * [13 years ago](https://wordpress.org/support/topic/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.inc
       ```
   
 * add
 *     ```
       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');
       ```
   
 * [http://wordpress.org/extend/plugins/contextual-related-posts/](http://wordpress.org/extend/plugins/contextual-related-posts/)

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Plugin Author [Ajay](https://wordpress.org/support/users/ajay/)
 * (@ajay)
 * [13 years ago](https://wordpress.org/support/topic/path-clear-cache-button/#post-3797596)
 * Thank you. Two quick questions.
 * 1. Do we need to use nonces to secure the response?
 * 2. You have `{action: 'crp_clear_cache'}` and `function crp_ajax_clearcache(){`
   
   Do these have to be same?
 *  Thread Starter [flynsarmy](https://wordpress.org/support/users/flynsarmy/)
 * (@flynsarmy)
 * [13 years ago](https://wordpress.org/support/topic/path-clear-cache-button/#post-3797597)
 * No nonce required. The response comes from WP AJAX url which is secure and we
   don’t do anything with it other than alert anyway.
 * The action ‘wp_ajax_crp_clear_cache’ triggers the wordpress event wp_ajax_crp_clear_cache
   which I’ve attached to the callback function crp_ajax_clearcache. You can rename
   the callback function to anything you like, that’s just what I named mine.
 * If you want to change the action name, you’ll need to change teh WP event name
   to match.
 *     ```
       {action: 'foo'}
       add_action('wp_ajax_foo', ...
       ```
   
 * See [AJAX in Plugins](http://codex.wordpress.org/AJAX_in_Plugins) for more information.
 *  Plugin Author [Ajay](https://wordpress.org/support/users/ajay/)
 * (@ajay)
 * [13 years ago](https://wordpress.org/support/topic/path-clear-cache-button/#post-3797600)
 * Thank you. That clears. I’m giving the code a test drive on my install
 *  Plugin Author [Ajay](https://wordpress.org/support/users/ajay/)
 * (@ajay)
 * [12 years, 12 months ago](https://wordpress.org/support/topic/path-clear-cache-button/#post-3797751)
 * This has been implemented in v1.8.8 that I released today

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘[PATH] Clear cache button’ is closed to new replies.

 * ![](https://ps.w.org/contextual-related-posts/assets/icon-256x256.png?rev=2985705)
 * [Contextual Related Posts](https://wordpress.org/plugins/contextual-related-posts/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/contextual-related-posts/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/contextual-related-posts/)
 * [Active Topics](https://wordpress.org/support/plugin/contextual-related-posts/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/contextual-related-posts/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/contextual-related-posts/reviews/)

 * 4 replies
 * 2 participants
 * Last reply from: [Ajay](https://wordpress.org/support/users/ajay/)
 * Last activity: [12 years, 12 months ago](https://wordpress.org/support/topic/path-clear-cache-button/#post-3797751)
 * Status: not a support question