All pastes #256491 Raw Edit

Anonymous

public text v1 · immutable
#256491 ·published 2006-11-24 08:18 UTC
rendered paste body
<?php
/* 
Plugin name: Random post link
Plugin URI: http://www.vituperation.com/ranpost
Description: A plugin to create a link to a random published post on your blog. Please report problems
to freds.plugins@gmail.com or by leaving a comment on the plugin site.
Author: Fred A.
Version: 1.0
Author URI: http://www.vituperation.com

INSTRUCTIONS

1. Copy this file into the plugins directory in your WordPress installation 
   (wp-content/plugins).
2. Log in to WordPress administration. Go to the Plugins page and Activate 
   this plugin.

Once you've activated the plugin, you can get a random post link by calling the 
fla_random_post() function. The function can be called one of two ways:

1. <?php fla_random_post("link text"); ?>
   This generates and prints an entire URL to a random published post in the 
   following format:

      <a href="http://link_to_random_post">link text</a>
 
   The displayed URL, including the anchor tags and link text as shown above, 
   is also returned as the function's value.


2. <?php fla_random_post(); ?>
   This generates a URL to a random published post in your blog, and returns 
   a fully qualified URL to you for you to use. It displays nothing.



    Copyright 2005  Fred Anderson  (email : fred.anderson@gmail.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/


function fla_random_post($text="")
{
  global $wpdb;

  $url = get_option('siteurl');
  $row = $wpdb->get_row("select id from $wpdb->posts".
                        " where post_status='publish'".
                        " order by rand() limit 1");

  if ($row) {
    if (strlen($text)) {
      $random = "<a href=\"$url/?p=$row->id\">$text</a>";
      echo $random;
    }
    else $random = "$url/?p=$row->id";
  }
  else $random = "";
  
  return $random;
}

?>