Hire a web Developer and Designer to upgrade and boost your online presence with cutting edge Technologies

Saturday, November 19, 2011

How To Display WordPress Sidebar On Other (Non WP) Sites

While working on my latest project, I came across the need to embed my entire WordPress sidebar on a non-WordPress site – specifically a bbPress forum. Since I make use of various widgets to display popular posts and other dynamic content, simply copying and pasting the HTML would not suffice. In the end, there are really only two ways of going about this:
  1. Include your wp-load.php file in your application and in effect load the entirety of WordPress to get access to the plugin functions
  2. Use simple caching to store the sidebar in HTML format and include it in any other application, then rebuild it as necessary when new content is published
If it’s not obvious enough, the first option is very costly in terms of database queries and can significantly slow your site down. The second option requires a little effort in implementation but performance wise there is no competition.

Caching Your WordPress Sidebar

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.   
  4.     // Configure files and directories:  
  5.     $cacheDir = TEMPLATEPATH."/cache";  
  6.     $cacheFileName = $cacheDir."/cache-$cacheFile.txt";  
  7.     $cacheLogFile = $cacheDir."/cache-log.txt";  
  8.   
  9.     // Make cache directory if it doesn't exist  
  10.     if(!is_dir($cacheDir)) mkdir($cacheDir, 0755);  
  11.   
  12.     // Make a log of the cache files with their current status  
  13.     if(file_exists($cacheLogFile))  
  14.     $cacheLog = unserialize(file_get_contents($cacheLogFile));  
  15.     else  
  16.     $cacheLog = array();  
  17.   
  18.     if($task == 'start'){ 
  19.         // If cache exists, is less than 6 hours old and is not in deletion queue, keep it - otherwise rebuild cache 
  20.         if(file_exists($cacheFileName) && (time() - filemtime($cacheFileName)) < $cacheTime && $cacheLog[$cacheFile] == 1){ 
  21.             $cache = false; 
  22.             } else { 
  23.             $cache = true; 
  24.             ob_start(); 
  25.         } 
  26.         }elseif($task == 'end' && $cache){ 
  27.         // If caching, save file contents and update log 
  28.         file_put_contents($cacheFileName,ob_get_contents()); 
  29.         ob_end_flush(); 
  30.         $cacheLog[$cacheFile] = 1; 
  31.         file_put_contents($cacheLogFile,serialize($cacheLog)); 
  32.         }elseif($task == 'purge'){  
  33.         // Set cache to delete and update log  
  34.         $cacheLog[$cacheFile] = 0;  
  35.         file_put_contents($cacheLogFile,serialize($cacheLog));  
  36.     }  
  37.   
  38. }  
  39. function cache_purge(){  
  40.     $cacheDir = TEMPLATEPATH."/cache";  
  41.     $cacheLogFile = $cacheDir."/cache-log.txt";  
  42.     if(file_exists($cacheLogFile))  
  43.     $cacheLog = unserialize(file_get_contents($cacheLogFile));  
  44.     else  
  45.     $cacheLog = array();  
  46.     foreach($cacheLog as $key=>$value)  
  47.     $cacheLog[$key] = 0;  
  48.     file_put_contents($cacheLogFile,serialize($cacheLog));  
  49. }  
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.
The second function is used to purge all cache files and is useful for adding to existing WordPress hooks. With it, we can make it so our cache is cleared every time our theme is updated, a post is saved or our sidebar widgets are updated by adding the following at the end of functions.php:
  1. add_action('switch_theme','cache_purge', 10);  
  2. add_action('publish_post','cache_purge', 10);  
  3. add_filter('widget_update_callback','cache_purge', 10);  
For a full list of hooks, check out the WordPress Plugin API Reference.

Configuring Your Sidebar

Now that you have the functions ready, you can begin caching your sidebar. Open up your sidebar.php and add this line to the beginning of the file:
  1. 'start''sidebar'); ?>  
This function will start caching the sidebar it needs to, otherwise it will do nothing. Because of the way the PHP output buffering function works, we also need to close off the buffer at the end of the file as well. Add this line to the very bottom of the same file:
  1. 'end''sidebar'); ?>  
After you’ve added those two lines, refresh your blog. You won’t see anything in your browser but check the/cache directory in your theme folder and you should see two files starting with cache-.

Using Your Cache

Now that you’ve created your cache file, you can use it in any application that has access to it. You can easily do this using the include_once function but I also like making sure the file exists, just to be safe:
  1.     $myCachedFile = "/path/to/cache-sidebar.txt";  
  2.     if(file_exists($myCachedFile))  
  3.     include_once($myCachedFile);  
  4.     else  
  5.     echo "Cache doesn't exist.";  
  6. ?>  
Just be sure to double-check your file path is correct. If you want to set your cache to delete from an external application, you’ll have to include the cache_purge function in your script somewhere and simply call that whenever necessary. After that it’s simply a matter of calling the function:
  1.   

Conclusion

This technique is really useful for any PHP application, especially when you want to display dynamic content without having to sacrifice performance. Use it to speed up your WordPress install or be able to use useful bits (like the sidebar) on any other website or application.
wp sidebar2 How to Display Wordpress Sidebar on Other (Non WP) Sites

No comments:

Post a Comment