This is a plugin/widget to show random quotes on your blog. Frank van den Brink originally created it and Christian Beer has also contributed. The wordpress.org homepage is here: Yet Another Random Quote.

I like this plugin, even if very few people around here can be bothered to actually click on a link to see my random quotes. Really, it’s okay. It’s just fine if you don’t give a rat’s ass about my collected pearls of wit, wisdom, and pop culture ephemera. (You probably won’t care to read my old YARQ post either.)

It appears that the plugin hasn’t been actively maintained, but through the miracle of free software I’ve been able to make my own fixes and enhancements, and now I can share them with you and the world. (I did get in contact with Christian, so we’ll see what we can do about updating the official plugin page.)

YARQ v3.0 works with WordPress 3.0 (for me, anyway), and I suspect will work with WP 2.5 and up. Please let me know how it goes for you. I can’t promise anything for time commitment, but I’d like to keep this thing alive so I welcome feedback and will try to fix reported problems.

Download

yarq.tgz (13K)

yarq.zip (14K)

YARQ is licensed with the GNU General Public License, version 2 or later.

Updated, 16 July 2010: In v3.0.1, detached customized functions to be used in functions.php if desired, or they can be ignored. This way the core plugin will be compatible for future updates. More below…

Updated, 17 July 2010: In v3.0.2, license reverts to GP version 2 or later. I think with WordPress being v2 only, the plugin would have to be v2 also. But if WordPress ever goes to v3 or higher, or if you use this code for something else, then I’d say v2+ should apply.

Updated, 19 July 2010: Still v3.0.2, but uses new %opt_separator% option instead of %opt_comma%. Christian updated the official wordpress.org plugin page and made this change. There are a few other small changes also.

Usage

  • Upload yarq.php to the /wp-content/plugins/ directory and activate through the Plugins menu in WordPress.
  • Add your own quotes via the admin page, Tools → Quotes.
  • Display the quotes by placing <php yarq_display(); ?> in your template, or using the widget (which I don’t use so haven’t confirmed if it works okay).
  • You can control the formatting of the quote in Settings → YARQ. For example, I use:

    %quote%
    <p>%author%%opt_separator%%source%</p>

    Where the %placeholders% will be replaced with the corresponding part of the quote. (These should be self-explanatory, except for %opt_separator% which is a new feature in this version and explained below.)

Fixes for Previous Official Version

  • The “Add Quote” form stopped working at some WordPress upgrade along the way. (I upgraded from 2.0 to 2.9 so it’s hard to say exactly when this broke.)
  • The admin page wasn’t showing the sources for the quotes.

Changes in v3.0

  • You can edit quotes!
  • “Add Quote” form moved to top, and “QID” (Quote ID) field added for editing existing quotes.
  • Author is now optional. If neither author nor source is given, then the %author placeholder will be replaced with “unknown”.
  • If a quote has both an author and a source, %opt_separator% will be replaced with the separator value set in YARQ options. (For example: a comma and a space.) Otherwise it will be replaced by an empty string.
  • Going against the random nature of this plugin, you can show a specific quote via a GET parameter in the URL, e.g.:

    http://www.movingtofreedom.org/random-quote/?qid=76

    You and your readers can find the ID for a displayed quote by looking at the page source, which will show something like this near the quote: <!-- id = 76 -->

    (intval is used to make sure only a numeric quote ID can be passed in, preventing a SQL injection attack.)

  • Auto-formatting is performed on the quote when adding it to, so that:

    One day I feel I’m ahead of the wheel
    And the next it’s rolling over me

    I can get back on
    I can get back on

    Goes into the database as:

    <p>One day I feel I’m ahead of the wheel<br />
    And the next it’s rolling over me</p>

    <p>I can get back on<br />
    I can get back on</p>

    If you edit the quote, it’s mostly smart enough to preserve br and p tags as they are and add new ones where necessary. With the nifty new edit feature, you can easily experiment to see how smart or dumb it is.

    You probably won’t get very good results with tags like ul or ol. Maybe we’ll need to add a checkbox to disable auto-formatting on the fly, but this handles most of my quoting needs. You can disable it altogether by commenting out this line in yarq.php:

    $quo = format_quote($quo);

  • Escape slashes are now hidden everywhere. \"Fred\'s\" may be stored in DB, but will be displayed as "Fred's".

“Off-Road” Features

I think these might be useful for others, but they’re experimental and individualized to my needs, so I used functions.php to separate out the custom parts. The plugin will check to see if the functions exist before trying to call them. If you don’t feel comfortable mucking around with the code, or you just simply don’t want to use them, no problem. Just ignore this and the plugin will work normally.

The Features

  • More fun with GET parameters in the URL! You can pick a random quote from a specific author (“who”) or source (“src”), e.g.:

    …/random-quote/?who=martin-sexton
    …/random-quote/?src=calvin-and-hobbes

    However! I wanted to limit this to authors and sources where I have more than one or two quotes. Also! When a quote is displayed from one of these “included” authors or sources, there will be a link from the name to look up another one.

    Remember, this is kind of half-baked. More work is needed to handle authors/sources that contain HTML, quote marks, ampersands, and etcetera. But! I think it’s safe and robust within its limitations, and it works for my purposes, which counts for a lot. :-)

  • I have an imbalance of Rush lyric excerpts, so I reduced the odds of getting a Rush quote. You may similarly want to cheat the gods of probability, or modify the SQL “where” clause in some other way.

The Code

In yarq.php, the yarq_display function makes these calls

/* ... */
if (function_exists('yarq_get_where_clause')) { $where = yarq_get_where_clause(); }
/* ... */
if (function_exists('yarq_get_author')) { yarq_get_author($quote->author, $output); }
if (function_exists('yarq_get_source')) { yarq_get_source($quote->source, $output); }
/* ... */

And in your theme’s functions.php file, you can create the functions like so:

yarq_get_where_clause()

Checks the URL GET parameters for author and source. Calls yarq_get_author and yarq_get_source to see if they are “included” (and buffer against using the raw GET data in the SQL query).

Also there is the probability manipulation with setting the odds of getting Neil Peart’s poetry at 1 in 8.

function yarq_get_where_clause()
{
	if (isset($_GET['who']) && function_exists('yarq_get_author')) {
		$author = yarq_get_author($_GET['who'], $output);
	}
	if (isset($_GET['src']) && function_exists('yarq_get_source')) {
		$source = yarq_get_source($_GET['src'], $output);
	}
	if (empty($author) && empty($source)) {
		// reduce the odds of getting Rush
		$rnd = rand(1,8);
		if ($rnd == 1) {
			$author = 'Rush';
		} else {
			$author_not = 'Rush';
		}
	}
	if (!empty($author)) {
		$where = " WHERE author = '$author'";
	} elseif (!empty($author_not)) {
		$where = " WHERE author != '$author_not'";
	} elseif (!empty($source)) {
		$where = " WHERE source = '$source' ";
	}
	return $where;
}

yarq_get_author()

// used in two ways: 1) see if an author is marked for the link treatment and
//                      return name for use in SQL, and
//                   2) replace author in output string with the link
//
// assumes no single or double quotes or html already in author name; just plain text
// will have to enhance for other scenarios if/when necessary
function yarq_get_author($a, &$output)
{
	$author_param = str_replace(' ', '-', strtolower($a));
	switch($author_param) {
		case 'author-one':		$author = 'Author One';		break;
		case 'author-two':		$author = 'Author Two';		break;
		// etcetera
	}
	if (!empty($author)) {
		$output = str_replace($author, '<a href="?who=' . $author_param . '">' . $author . '</a>', $output);
	}
	return $author;
}

yarq_get_source()

(Same comments apply as for yarq_get_author.)

function yarq_get_source($s, &$output)
{
	$source_param = str_replace(' ', '-', strtolower($s));
	switch($source_param) {
		case 'source-one':		$author = 'Source One';		break;
		case 'source-two':		$author = 'Source Two';		break;
	}
	if (!empty($source)) {
		$output = str_replace($source, '<a href="?src=' . $source_param . '">' . $source . '</a>', $output);
	}

	return $source;
}