WordPress.org

Ready to get started?Download WordPress

Forums

Contextual Related Posts
[PATH] Clear cache button (5 posts)

  1. flynsarmy
    Member
    Posted 1 year ago #

    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/

  2. Ajay
    Member
    Plugin Author

    Posted 1 year ago #

    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?

  3. flynsarmy
    Member
    Posted 1 year ago #

    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 for more information.

  4. Ajay
    Member
    Plugin Author

    Posted 1 year ago #

    Thank you. That clears. I'm giving the code a test drive on my install

  5. Ajay
    Member
    Plugin Author

    Posted 1 year ago #

    This has been implemented in v1.8.8 that I released today

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.