Random Posts plugin

September 11th, 2006 | by jg3 |

As you may notice down on the left, I have added the WASABI WordPress Random Posts plugin. It seems succinct and well written for what it is, a PHP function written around a SQL query. What it is not, however, is documented. There are a couple of comments to this effect. Jeff says:

After all the time it takes to write a plugin, how hard can it be to write one paragraph explaining how to use it?

And .|deuce|. ramps it up a bit with this rant:

I’m sure this plug-in is useful, but to the average blogger, it’s just another piece of insider code … I know there are plenty of people who are indifferent to anyone but the techno-elite, … but elitist code ends up as little more than self-indulgent …

Of course, there are lots of comments to the effect of “good job” and “thanks for this work”, and so on. It seems that this plugin really works for people who could have written it themselves in 2-4 hours of tinkering with PHP and SQL. For those who could never have written it, it will probably take 2-4 hours of tinkering to get it to work.

So, here is some hopefully useful notes on how I got it working.

drwxrwxr-x  2 root  www   512 May 28  2004 __MACOSX
-rw-r--r--  1 root  www   564 Oct 20  2004 random-posts-readme.txt
-rw-r--r--  1 root  www  1424 Oct 20  2004 random-posts.php
-rw-r--r--  1 root  www  1301 Jan 11  2006 random-posts.zip

After unzipping the archive, I knew from experience that the __MACOSX directory was garbage and could be deleted. Then I read the *readme.txt file hoping for a few pointers on getting it running.

 % cat random-posts-readme.txt
 Random Posts 1.1

 random_posts($limit, $len, $before_title, $after_title, $before_post, $after_post, $show_pass_post, $show_excerpt);

 $limit - No. of posts to show.
 $len - Lenght of auto-generate excerpt.
 $before_title - Text to appear before the entry title.
 $after_title - Text to appear after the entry title.
 $befor_post - Text to appear before the entry excerpt.
 $after_post - Text to appear after the entry excerpt.
 $show_pass_post = Include/exclude password protected entries (Default: false).

Hmm… no help there. So, let’s try and call that function and see if it works.

The first problem I had was figuring out where to put the random-posts.php file so that the function inside would be available when called by a wordpress page. After a little poking around I buried it alongside my other plugins in the ./wp-content/plugins/ directory. You will know you haven’t put your plugin file in the right place or permissions on the file are incorrect if you see an error like this in your php-error.log file:

[10-Sep-2006 22:56:26] PHP Fatal error:  Call to undefined function:  random_posts() in /www/log.earlax.com/data/wp-content/themes/myperminimalist/sidebar.php on line 91

For some reason, ./wp-includes/ made sense to me, but I am here to tell you that’s not the place to put this file. Put it in your ./wp-content/plugins/ directory. And then, oh yeah, I had to go back to the WordPress admin interface and activate the plugin there (yeah, its been a while).

Now, since I want to have the random link show up in my “sidebar” so in my WordPress theme editor I start hacking away in the sidebar.php file. (It would be a good idea to make a backup of this file first!). Here’s what I wound up with:

<div id="random_post">
  <h3>< ?php _e('Random Old Post:'); ?></h3>
  < ?php random_posts(1, 0, '', '<br />', '', '', 0, 0); ?>
</div>

You will notice that the first and last line start and end the code chunk that the random posts call lives in. The second line generates the title of the section according to my stylesheet (I’m not sure exactly why it is done this way, but I’m following the example of the existing elements of my theme). Then the third line is where the work is done. You will notice that a parenthetical expression following random_posts contains a list of elements separated by commas. These correspond to the variables explained in the *readme.txt file. When I first tried this, my list was just (1) as I was hoping to get just one random link and rely on defaults for the rest. That caused me to get this error in my php-error.log:

[10-Sep-2006 22:59:39] PHP Warning:  Missing argument 2 for random_posts() in /2/www/log.earlax.com/data/wp-content/plugins/random-posts.php on line 11

So from that I figured out that I needed at least two, and probably all of the elements of the function specified. Maybe they don’t all have to be, but I found that if I did not at least specify that I wanted ” (nothing) for the $before_title and $after_title elements I wound up with strange bullet points at the beginning of each line for the rest of the sidebar.

Then, finally, I took updated the SQL query at the heart of the plugin following this suggestion posted in the comments of the plugin page. This allows me to specify that the randomly selected post must be at least 45 days old:

$sql = "SELECT ID, post_title, post_content FROM $tableposts WHERE post_status = 'publish' AND post_date < DATE_SUB(CURDATE(), INTERVAL 45 Day) ";

So there. Maybe that isn’t exhaustive, but it is at least a few notes based on my successful experience. Filed under:

Related stuff

You must be logged in to post a comment.