WordPress plugin - “Get Random Links from Link Manager”

getrandomlinks.phps

It’s a wordy title, but explains exactly what happens: this plugin provides a function to pull any number of links you like - at random - from the Link Manager database. There’s another random-links plugin available for WordPress, but that requires you to fill up a textfile with links to choose at random. Now, the link manager does a pretty good job of cataloguing links and, besides, having data in two places is just bad.

So I wrote this plugin to overcome that problem. Dead simple to use: download the file (here, if you haven’t done so already), save it as get_random_links.php and upload that to the plugins directory, under your wp-content directory. Go to the Plugins page in the WordPress dashboard and activate it.

Then you can use the get_random_links function in any of your templates. The source code does document the function within it, but for quick reference (and if you want to see if this is the function for you), here’s a quick rundown:

get_random_links($number, $echo, $before, $after, $descr, $between)

$number - number of links to pick. Defaults to 5.
$echo - print output to browser or not. defaults to 1 (TRUE). Function also returns an array of the form array[$i][link_id], etc, where $i is the index of the individual link
$before - code to prefix each link with; defaults to <li>
$after - code to suffix each link with; defaults to </li>
$descr - display description with link or not. Defaults to 0.
$between - what code to display between the link url and the description. Defaults to <br/>; note that $after is displayed after the description - right at the end of the loop

The link itself is displayed as <a href=”linkURL” title=”linkname”>linkname</a>.

So, to show three links, with description (seperated by a break tag) and each link being within its own paragraph type, you’d call the function thus:

get_random_links(3, 1, "<p>", "</p>", 1, "<br/>");

But you don’t have to echo the output. The function also returns a multidimensional array of all the data it collects, stored thus - $array[$i][link_url] $array[$i][link_name] $array[$i][$link_description] - where $i is the index of the set of results (eg $array[0][link_name] would correspond to the title of the first link returned.

I think that’s it. Any bug reports are welcome to tom at infovore dot org.

Endnotes