Static and Dynamic, Sitting in a Tree
As much as I love the dynamic nature of WordPress, sometimes dynamic is not the best way to go. Take for instance, my “recent entries” section (currently at the top left). This was being done by using Nick Momrick’s Recent Posts plugin (with some slight modification as to the way it is output). But this was being done dynamically every time someone loaded a page. Each time it had to connect to the database and pull out the list of recent entries. I’m not posting once every 5 minutes here, so I didn’t see any need for this data to be dynamic.
Good thing WordPress has a great new plugin API. It is insanely easy to create a plugin that is only executed when a post is published, edited, or deleted. So all I did was make a plugin that writes to a file the list of the 20 most recent entries every time I publish, edit, or delete a post. It happens in a heartbeat, and I cut the number of database queries I was making on my main page from 50 down to 29. Now, I just include the static text file that is dynamically updated. Remember dynamic and static aren’t meant to be exclusive. Sometimes it makes sense to cache things statically, and sometimes it makes sense to pull it dynamically from the database.
Here’s the code I am using. I just edited Nick’s plugin to suit my needs. I did it this way because if you have a plugin that calls another plugin, you have to make sure that one is defined before the other.
<?php
/*
Plugin Name: Recent Posts Static
Version: 1.0
Plugin URI: http://wiki.wordpress.org/index.php/Recent%20Posts
Description: This will return titles of the most recent posts with a permalink to the actual post itself<br /><br />The default options will display the 5 most recent posts
Author: Nick Momrik and Mark Jaquith
Author URI: http://mtdewvirus.com/
*/
function get_recent_posts($no_posts = 5, $before = '<li>', $after = '</li>', $show_pass_post = false, $skip_posts = 0) {
global $wpdb, $tableposts;
$request = "SELECT ID, post_title, DATE_FORMAT(post_date, '%b %d') AS post_date2 FROM $tableposts WHERE post_status = 'publish' ";
if(!$show_pass_post) { $request .= "AND post_password ='' "; }
$request .= "ORDER BY post_date DESC LIMIT $skip_posts, $no_posts";
$posts = $wpdb->get_results($request);
$output = '';
if ($posts){
foreach ($posts as $post) {
$post_title = stripslashes($post->post_title);
$permalink = get_permalink($post->ID);
$post_date = stripslashes($post->post_date2);
$output .= $before . '<span class="entrydate">' . $post_date . '</span> <a href="' . $permalink . '" rel="bookmark" title="Entry: ' . $post_title . '">' . $post_title . '</a>' . $after;
}
}
return $output; // return it, so we can save the output to a file
}
// function to save the recent entries list as a file by Mark Jaquith
function wp_static_recent_entries(){
$filename = '/home/txfx/www/www/includes/wp_recent_entries.txt';
$fp = fopen($filename, "w");
$string = get_recent_posts(20);
fputs($fp, $string);
fclose($fp);
}
add_action('publish_post','wp_static_recent_entries',9);
add_action('edit_post','wp_static_recent_entries',9);
add_action('delete_post','wp_static_recent_entries',9);
?>
You’ll want to modify the output to suit your tastes… as you can see I have the entry date shown alongside the link… but this should be enough to set you on the right path toward creating dynamically updated static data chunks.
