• Resolved mgcwebsites

    (@mgcwebsites)


    Many of my sites feature bespoke plugins to allow junior staff to safely add structured content to their site using a login with Author permissions.

    The plugins are then triggered via a short code to add content via php to some or all of the site pages.

    As such I need a way for my plugins to clear the WordFence cache automatically when records are inserted or modified. Or, at worst, I need to make the clear cache button available to users with Author permission.

    I see there is a basic API but it does not seem to include this feature.

    I appreciate there are other solutions using JS but that isn’t the way I want to go for many reasons.

    https://ww.wp.xz.cn/plugins/wordfence/

Viewing 8 replies - 1 through 8 (of 8 total)
  • wfasa

    (@wfasa)

    Hello mgcwebsites,
    Wordfence cache is cleared when a post is saved. Does the actions your users are doing not trigger a save of posts?

    Thread Starter mgcwebsites

    (@mgcwebsites)

    Thanks wfasa. No my plugins add/update/delete content in separate tables. If I knew exactly how WordFence recognised a post/page update perhaps I could trick it into thinking there had been one?

    wfasa

    (@wfasa)

    We might be able to figure something out but explain to me how that data is cached if it’s not in a page or post? How are you displaying the data that they are entering on your website?

    Thread Starter mgcwebsites

    (@mgcwebsites)

    The plugins use shortcodes in the page that trigger php functions that insert html, e.g. that days entertainment or opening hours etc..

    I assume this output is being cached as part of the page when it is first rendered and from then on the shortcodes aren’t being utilised?

    If so then if I could just trigger a clear of the cache I’d be away. Or have I miss-understood?

    Ah yes I see what you are doing! If the interface through which they are saving the “extra” data is within WordPress scope you should be able to fix it by simply clearing cache at that point.

    There is more information about how to do that here.

    Thread Starter mgcwebsites

    (@mgcwebsites)

    Thanks that’s the info I was looking for. I’ll try and leave an example to finish this thread off when I have got it sussed in a day or two.

    Thread Starter mgcwebsites

    (@mgcwebsites)

    Here’s my code which is working well though I haven’t been able to test the error handling as I haven’t yet managed to generate one. The code relies on my print_admin_notice function (which is pretty self explanatory) and would obviously be easily broken by changes to WordFence so would not really be suitable for redistributed plugins in this form. I hope this helps somebody. Thanks for your help wfasa.

    function clearWordFenceCache(){
    	if(is_plugin_active('wordfence/wordfence.php')){
    		if(wfConfig::get('cacheType', false)!=''){
    			$result = wfCache::clearPageCache();
    			if ($result['totalErrors'] > 0){
    				foreach ($result['error'] as $error){
    					print_admin_notice('error', 'Clearing the WordPress cache failed with the following error: '.$error);
    				}
    			}else{
    				print_admin_notice('info', 'The WordFence Cache has been cleared. '.$result['dirsDeleted'].' directories and '.$result['filesDeleted'].' files deleted.');
    			}
    		}else{
    			print_admin_notice('info', 'The WordFence plugin is active but the cache is disabled so needn\'t be cleared');
    		}
    	}else{
    		print_admin_notice('info', 'The WordFence plugin is either disabled or not installed so the cache needn\'t be cleared');
    	}
    }

    Thanks for sharing your solution mgcwebsites! Very thoughtful.
    Setting this to resolved for now.

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

The topic ‘Make my bespoke plugins Wordfence cache friendly’ is closed to new replies.