<?php
/*
Plugin Name: Random Links from Link Manager
Plugin URI: http://infovore.org/code/wordpress-plugin-get-random-links-from-link-manager/
Description: Adds a new template function, get_random_links, which picks a specified number of links (which have been described as visible) at random from the links in the Link Manager database. Returns values in an associative array/hash and (by default) echoes them. For further explanation please see the source or the page about the plugin.
Version: 1.0
Author: Tom Armitage
Author URI: http://www.infovore.org
*/


/* An explanation of the function:
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>.
*/

function get_random_links($number 5$echo 1$before "<li>"$after "</li>"$descr 0$between "<br/>") {
        global 
$wpdb;
        
$results $wpdb->get_results("SELECT link_url, link_name, link_description FROM $wpdb->links WHERE link_visible = 'Y' ORDER BY rand() LIMIT ".$number);

    foreach (
$results as $result) {
            
// fill out variables from database
            
$link_url $result->link_url;
            
$link_name $result->link_name;
            
$link_description $result->link_description;    

            
// if the links are to be displayed:
            
if ($echo == 1) {
                
                echo 
$before;
                echo 
'<a href="'.$link_url.'" title="'.$link_name.'">'.$link_name.'</a>';
                if (
$descr == 1) {
                    echo 
$between;
                    echo 
$link_description;
                }
                echo 
$after;
            }

            
// fill out an array with the variables
            
$tempvars[link_url] = $link_url;
            
$tempvars[link_name] = $link_name;
            
$tempvars[link_description] = $link_description;

            
// add that temporary array to our big array of returns...
            
$returnvars[] = $tempvars;
    }
return 
$returnvars;
}
?>