How to Display WordPress Sidebar on Other (Non WP) Sites
by Paul Ogier on Aug 17, 2010 • 7:16 pm No CommentsBefore 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:
- function cache($task, $cacheFile, $cacheTime = 21600){
- global $cache;
- // Configure files and directories:
- $cacheDir = TEMPLATEPATH."/cache";
- $cacheFileName = $cacheDir."/cache-$cacheFile.txt";
- $cacheLogFile = $cacheDir."/cache-log.txt";
- // Make cache directory if it doesn’t exist
- if(!is_dir($cacheDir)) mkdir($cacheDir, 0755);
- // Make a log of the cache files with their current status
- if(file_exists($cacheLogFile))
- $cacheLog = unserialize(file_get_contents($cacheLogFile));
- else
- $cacheLog = array();
- if($task == ‘start’){
- // If cache exists, is less than 6 hours old and is not in deletion queue, keep it – otherwise rebuild cache
- if(file_exists($cacheFileName) && (time() – filemtime($cacheFileName)) < $cacheTime && $cacheLog[$cacheFile] == 1){
- $cache = false;
- } else {
- $cache = true;
- ob_start();
- }
- }elseif($task == ‘end’ && $cache){
- // If caching, save file contents and update log
- file_put_contents($cacheFileName,ob_get_contents());
- ob_end_flush();
- $cacheLog[$cacheFile] = 1;
- file_put_contents($cacheLogFile,serialize($cacheLog));
- }elseif($task == ‘purge’){
- // Set cache to delete and update log
- $cacheLog[$cacheFile] = 0;
- file_put_contents($cacheLogFile,serialize($cacheLog));
- }
- }
- function cache_purge(){
- $cacheDir = TEMPLATEPATH."/cache";
- $cacheLogFile = $cacheDir."/cache-log.txt";
- if(file_exists($cacheLogFile))
- $cacheLog = unserialize(file_get_contents($cacheLogFile));
- else
- $cacheLog = array();
- foreach($cacheLog as $key=>$value)
- $cacheLog[$key] = 0;
- 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
