Before we can write our function, we need to understand exactly what we’re trying to do. Our goal is to cache our sidebar to a text file and update that cache whenever we publish a post, change our theme or make changes to the sidebar widgets. Since we’re planning on using our cache to display the sidebar on a different application, we need to be able to easily delete the cache without any ill-effects (we don’t want our sidebar broken at any time).

To accomplish this efficiently, we’ll also create a log of our cached files and use that to determine whether the cache should be cleared. If so, the actual cache file will be overwritten the next time someone visits the WordPress site, ensuring a proper sidebar is always rendered on both applications.

Creating the Functions

To get started, open up your theme’s functions.php file and add the following functions to it:

  1. function cache($task, $cacheFile, $cacheTime = 21600){ 
  2. global $cache; 
  3. // Configure files and directories:
  4. $cacheDir = TEMPLATEPATH."/cache"; 
  5. $cacheFileName = $cacheDir."/cache-$cacheFile.txt"; 
  6. $cacheLogFile = $cacheDir."/cache-log.txt"; 
  7. // Make cache directory if it doesn’t exist
  8. if(!is_dir($cacheDir)) mkdir($cacheDir, 0755); 
  9. // Make a log of the cache files with their current status
  10. if(file_exists($cacheLogFile)) 
  11. $cacheLog = unserialize(file_get_contents($cacheLogFile)); 
  12. else
  13. $cacheLog = array(); 
  14. if($task == ‘start’){
  15.         // If cache exists, is less than 6 hours old and is not in deletion queue, keep it – otherwise rebuild cache
  16.         if(file_exists($cacheFileName) && (time() – filemtime($cacheFileName)) < $cacheTime && $cacheLog[$cacheFile] == 1){
  17.             $cache = false;
  18.             } else {
  19.             $cache = true;
  20.             ob_start();
  21.         }
  22.         }elseif($task == ‘end’ && $cache){
  23.         // If caching, save file contents and update log
  24.         file_put_contents($cacheFileName,ob_get_contents());
  25.         ob_end_flush();
  26.         $cacheLog[$cacheFile] = 1;
  27.         file_put_contents($cacheLogFile,serialize($cacheLog));
  28.         }elseif($task == ‘purge’){ 
  29. // Set cache to delete and update log
  30. $cacheLog[$cacheFile] = 0; 
  31. file_put_contents($cacheLogFile,serialize($cacheLog)); 
  32.     } 
  33. function cache_purge(){ 
  34. $cacheDir = TEMPLATEPATH."/cache"; 
  35. $cacheLogFile = $cacheDir."/cache-log.txt"; 
  36. if(file_exists($cacheLogFile)) 
  37. $cacheLog = unserialize(file_get_contents($cacheLogFile)); 
  38. else
  39. $cacheLog = array(); 
  40. foreach($cacheLog as $key=>$value) 
  41. $cacheLog[$key] = 0; 
  42. file_put_contents($cacheLogFile,serialize($cacheLog)); 

These first function, cache, is the key to getting our cache to work. Depending on where in your file you call it, the function will set up the correct files and directories, verify a cache needs to be built and if so, will save the output and update the cache log. The function uses PHP’s output buffering to save any HTML generated into a text file.

Read more: How to Display WordPress Sidebar on Other (Non WP) Sites http://www.hongkiat.com/blog/display-wordpress-sidebar-on-other-non-wp-sites/#ixzz0wstclM56

About Paul Ogier

Paul Ogier has written 202 posts.

Paul Ogier is an IT Guru, Website Developer, and generally a nice guy. He loves code and he loves design.