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.
Hmmm, from 50 to 29. Reduced to 58%. I wonder if that would hold proportionally with higher traffic?
Sure would. Each visitor to my main page only triggers 29 calls to my database (actually, 23 right now… it fluctuates as the 10 entries shown on my main page get comments, or get pushed off the main page). PHP and MySQL both scale very well, but keeping redundant database calls to a minimum certainly can’t hurt.
I am having some trouble getting this to work. Everything checks out, the text file is filled with information, but it just does not show on the site.
If the text file is filled with information, all you have to do is “include it.” For example, if your file was located at /home/site/www/www/path/file.txt:
would display it.
Hmm, from the code I can see that the file is wrtitten to every time a post is posted/edited/deleted … so you don’t call wp_static_recent_posts in your template and instead include the cached txt file?
I’m going to try this plug-in as I just tried the reg. recent posts for 1.5 and it broke the page, just white I had it working OK when the blog was a 1.2.1, but not now.
BTW — What plugins are you using here for the quick tag buttons, live preview and spell check? I have live preview working but it didn’t show the changes from the only quick tag plugin I could find. Thanks!
Okie, I did the comment quicktags myself, but there is a plugin here.
That’s the one I tried and it didn’t show the changes in the text effected by the quick tags, but yours does.
Nice job! You should make it a plug in. Thanks for the reply.
And if I can’t get this to write the TXT file??
Tried creating the text file manually so there was something there but it’s just not picking it up? The path is correct in the script but nothing.
This a 1.5 issue? Sorry getting slightly out of my depth here.. any advice is appreciated.
And yes, I realise it’s been a while since you posted this!!
Gordon,
You probably need to make the file writable…
$ chmod 666 wp_recent_entries.txt
On some servers, the permissions will be different.Ooh This is SWEEET – very nice ….
I would love id i could show the firs like 50 words of the post and a more link … could some one give a hint ?
use
the_excerpt();
instead ofthe_content();
Ehmm ok – the excerpt
$output .= $before . ” . $post_date . ‘ ‘ . $post_title . ‘‘ . $after;
}
this is the line that generate the txt can i just add the the_excerpt here ??
I’d love to give this a try as the hits on my database are kicking in a lock down on my server – which I’m anxious to change. Until then, have you updated this and will it work for WordPress 2+?
Thank you!
Lorelle,
I’m still using that here, and the concept is broadly applicable. You just change the function called, the filename written to, and maybe the hooks that it’s called on. Best thing to do is print out the queries made on each page and then try to eliminate the ones that don’t change very often (and thus don’t need to be run dynamically).
Mark: I’ve been battling with this plugin for a bit. I now understand that the text file must be writable, and the call to the plugin results on the template file is through the php
include
not theget_recent_posts()
, right?When I edit a post, I get massive header errors that are basically saying the file won’t open, as well as an error in pluggable-functions.php on line 272.
Does the plugin create the file or I need to create it? It didn’t work without a text file so I created a blank text file and set it to 666 and 777. Same errors. The path is right.
For now, I want this to work on recent posts, and I know I’m missing something.
Lorelle, make sure you change the filename of the cache file. You can use ABSPATH to represend WP’s path or TEMPLATEPATH to represent your theme’s path.
e.g.
$filename = ABSPATH . '/wp-content/recent.txt';
… would put it in your wp-content directory.
And yes, to display it, you would just include the txt file.
Lucky. They britney spears fakes britney spears fakes http://tlurl.com/?6QJsb50s britney spears fakes had placed a very efficient – old straw, if. Thank you have britney spears video britney spears video http://tlurl.com/?ZOzUvKh3 britney spears video many questions. But it and. The arm, captain who came to his britney spears dress up games britney spears dress up games http://tlurl.com/?Z9v8GXJY britney spears dress up games potty. Is that you know is, mother confessor.
You be your way here comes crazy monster cock tananda! I.
www disneychannel com