WordPress Plugin, YARQ v3.0: Yet Another Random Quote

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 care not one bit about my collected pearls of wit, wisdom, and pop culture ephemera. (You probably won’t care to read my old YARQ post either.)

It appeared that the plugin hadn’t been actively maintained, but through the miracle of free software I was able to make my own fixes and enhancements, and now I can share them with you and the world. (And I did get in contact with Christian who has added me as a contributor to the wordpress.org 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 v3.1 (14K)

Also available from the official wordpress.org plugin page.

The plugin has been tested with WordPress 3.0 and is licensed with the GNU General Public License, version 2 or later.

Updates

20 November 2010: v3.1: New function yarq_display_all(). Widget option to set title. No auto-format if <li> tag found in quote.

10 October 2010: v3.0.3: Added an option to disable the autoformat feature.

19 July 2010: Still v3.0.2, but uses new %opt_separator% option instead of %opt_comma%. There are a few other small changes also.

17 July 2010: v3.0.2: License reverted 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.

16 July 2010: 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…

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. (The widget has an option to set the title, e.g. “Random Quote”.)
  • 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.)

  • Display all quotes with <php yarq_display_all(); ?>. Optional parameters:

    1. $order_by: id, author, source, or quote (default = id)
    2. $asc: sort ascending, true or false (default = true)
    3. $format: override format set in admin options

    You might call from your theme/template file like so, supplying the <ul> or <ol> wrapping tags yourself:

    <?php if ( function_exists('yarq_display_all') ) { 
      echo '<h2>All Quotes</h2>';
      echo "<ul>\n";
      yarq_display_all('id', false);
      echo "</ul>\n";
    }

    Which can be styled as desired. Alternating <li> items, starting with the first, will use the class “yarq-alt”. (<li class="yarq-alt">)

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://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 or editing it, 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.

    Auto-formatting won’t be applied if <li> tags are found in a quote, and there is an admin option to turn off this feature altogether.

  • 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;
}

42 thoughts on “WordPress Plugin, YARQ v3.0: Yet Another Random Quote

  1. I think it would be interesting if YARQ could show “quotes of the day”, u know, the same day the same quote, not different for two visits in one day.
    :)

  2. This is a great plug-in. Using it to add a random series of jokes for a comedian’s site I’m doing. Could someone please point in the right direction to stylizing the output? Thanks so much for this.

  3. Thanks, Ryan. There are no classes or IDs in the output of the plugin. It’s up to you how you want to wrap it. For example, I have:

    <div id="yarq" class="big-text">
    <?php if ( function_exists('yarq_display') ) { 
    	yarq_display('«format»');
    } else {
    	echo "Oh, no! The YARQ plugin was not found.";
    } ?>
    </div>
  4. I tried to add the snippet to my header, but it won’t work there. It works fine in the sidebar.. seems the header doesnt includes the widget-files..?

  5. Hi

    i wonder if its just me…

    i am currently using a black background template.

    the random quote has a highlighted white background..

    can any one concur?

    thanks

  6. Hey Scott-

    Again, great plugin. Sorry I never got back to you on your answer. Thanks for that though. Maybe I’m missing something, but is it possible to list a bunch of jokes and have them rotate out every 10 seconds or so? It is possible to do this with this awesome plugin?

    Thanks Scott.

  7. That’s what I thought Scott and unfortunately…I’m clueless about how to use javascript for this. What a great plugin though. Maybe I can find a way soon. Thanks for the quick response Scott.

  8. Is it possible to remove the “Random Quote” headline above the quote itself? This is slick and easy to use, client just wants a quote and no title.
    Thanks Scott.

  9. Hi, Joel. The plugin doesn’t add any headline when called with <php yarq_display(); ?>. It’s all up to how you use it in your theme file. (If you look at my random quote page, there’s no headline/title.)

    Are you using it as a widget? I’ve yet to try that, and don’t know what your options are there.

  10. hey,
    I would like the format for the quotes to be something like %quote% – %author% so that I can display something like “this is my quote” – Anon

    However, it seems that %quote% is wrapped in tags which prevents me from having the author displayed on the same line? Is there a way around this? I will edit the plugin code if necessary, but would rather not so that future updates don’t mean I have to do the process again.

    Cheers,
    Justin

  11. Hi, Justin — Right now the only way I can think of is to manually comment out the auto-format code. On future revisions I might see about adding an option to disable that — I don’t think it would be too hard to implement.

  12. Ok, cool. I’ve manually removed the auto-format code for now, but I think the ability to remove it through the WordPress admin page would be a great feature for future releases.

    Cheers Scott.

  13. Your interest in this has inspired me to add the option just now. :-)

    I think I have it right, but if you want to get an early look and beta test it, I’d be happy to email it to you.

    Not sure how soon I’ll get this page and the wordpress.org site updated — maybe soon, maybe later!

  14. Your YARQ works like a charm in my WP 3.0.1. Just a thought as I’m using this plugin for random testimonials… In addition to listing a random quote on each page, my client wanted a page to view ALL the quotes. So I created a function to do just that using code cribbed off yarq.php and plopped into my template’s function.php. You can view the result here: http://im-safe.com/new/client-quotes/

    I’m sure I’m not the only person looking for such functionality, so that would be a great addition to the plugin “out of the box”.

  15. Hi Scott: Glad you liked my humble suggestion. Here’s another… The same client loves the random quote feature, but wants to call it something other than “Random Quote”. I went to where logic tells me I should be able to edit that title (admin / widgets) only to be told “There are no options for this widget.” So how about it? Custom title, ala the “Recent Posts” plugin? Thanks for all your work!

  16. Hey Scott: Back to the page to view ALL the quotes suggestion above, feel free to email me and I’ll send you the function I created. Not sure if that’ll help in implementing it into your next version, but it’s the least I can do!

  17. Hi, Frank. Thanks — I’ve started on the work and have it outputting the entire quote list as a list. Just need to experiment with it to make sure it’s stylable enough.

    For the widget, I’m still ignorant of how they work, including widget options. Seems you could modify the widget_yarq_register function to use a different string and that might do. I’d be happy to add the option code if you can come up with it.

  18. Frank: I found some widget code at:

    http://codex.wordpress.org/Widgets_API

    Which provides the option for setting the title, and that’s set and seems to work fine.

    The “all quotes” feature is pretty much done also, although I started wondering if you were looking for that as a widget feature also? I have it as a call from a theme file: yarq_display_all().

    I’ll be updating the docs and creating a new release, and maybe have everything done this weekend.

  19. Hi Scott,

    Love your plugin! Just one thing:

    On the quotes management page in the backend, the quotes listed overlap the WordPress footer. So at about the second or third quote listed the WP footer (“Thank you for creating with WordPress”) appears underneath a quote, obscuring the quote text.

    A formatting issue somewhere in the plugin code?

    Take care,
    Marcus

  20. Hi, Marcus. Thank you.

    And, you’re right. I think I half-noticed that recently but didn’t seize on it. My brain obviously didn’t want to deal with it.

    As a quick workaround, I just added:

    echo '<div style="clear: both; margin-bottom: 3em;">';

    to the very end of the yarq_admin_manage() function, and that seems to have taken care of it for me. That’s probably all I’ll do with it for now. This (or something more elegant) will show up in some future version.

  21. Hey,

    Just found YARQ – it works really well!

    Feature suggestions – in no particular order:

    * Make titles on the settings page BOLD!
    * Move the quotes from tools to settings (same page as is currently used for formatting if possible).
    * On the plugins page, add a link to the plugin settings.
    * Keep it simple!

    But they are just a few niggles, the end user doesn’t see them! Or I might just take a fork of this and do all that myself :)

  22. Thanks, Ben. I notice that “Format” and “Separator” on the settings page are surrounded by a legend tag — I wonder if that used to be styled differently, because I thought they stood out more before. But I bolded them along with “Autoformat quotes” so that will show up in some future version.

  23. I’m in WP version 3.3.1 – The plug-in doesn’t seem to be working (installed correctly, quotes added, php string added to page). Is there an easy fix?

    Thanks!

  24. love this plugin – i’ve integrated it into a header and tho it took me some work to get the CSS correct, the plugin itself is great.

    one little glitch i’ve found, and haven’t been able to sort out – there’s a mystery quote that’s empty that keeps displaying as just the ~ before the author name… no quote, no error, no nothin… just the ~ and i’ve checked the database there are no empty records

    any thoughts? it’s making me nuts, and client isn’t going to accept “i have no idea how to fix that” :D

  25. Hi, kera — when you say “~ before the author name”, do you mean the author of a quote? (So there is a quote entry?) What is the ~? Is that what you’re using as a separator?

  26. Big Thanks for the plugin. Although i am not using it for quote purpose :P its a great plugin for me to display random (actually some selected only) post titles in the sidebar.

  27. Hello,
    I really like your plugin. However, I would like to style the CSS (especially change the width of the quote box in a widget area) and I am not sure what widget id I should put in the CSS code. Could you please tell me if it is possible to change the width of the widget and what ID I should use. I would greatly appreciate it.
    Thank you very much in advance.

  28. Hello Scott,

    I’m using your great plugin in my header. I would like to know if there is a way to see a specific quote on my website to make sure that it’s displaying correctly? The reason for that is because I’m using different CSS class for long or short quotes and I want to make sure that it is fine on my website.

    Thanks a lot!

Comments are closed.