Random quotes in wordpress

By Shamus Posted Saturday Jun 3, 2006

Filed under: Programming 16 comments

For ubu roi and anyone else who is interested in a few of the tricks used on this site, here are a few bits of code that might help you out. Sadly, you can’t really do this stuff as plugins, which means you have to edit the code yourself. This will either be very easy or a little confusing, depending on how comfortable you are with PHP. Also, this code is only going to be useful if you’re using wordpress.

First, the random quote. Create a quotes file named “quotes.txt” and put it in the /wp-content filder. The file should have three lines for every quote: The first is the quote itself, the second is the person being quoted (leave blank if you dan’t want this) and the third is a required blank line. Mine looks like this. Now, somewhere in the PHP code for your site (I suggest at the top of the header.php) place this code:


<?php 
/*----------------------------------------------------------------------------- 
Pull out a random quote from the quote file and display it. 
-----------------------------------------------------------------------------*/ 
function random_phrase () 
{ 
$quotes = file ("wp-content/quotes.txt");
$num = rand (0, intval (count ($quotes) / 3)) * 3;
echo $quotes[$num] . "<br>" . $quotes[$num + 1];
}
?>

Almost done. Now just put this little bit of code where you want the quote to appear:

<?php random_phrase (); ?>

 


From The Archives:
 

16 thoughts on “Random quotes in wordpress

  1. awesome! I am definitely going to use that.

    Can we get teh code for the small dice roller at the bottom of your sidebar next?

  2. ubu roi says:

    Yep, I was going to extract my old quote file and use it.. .then I remembered it wasn’t on Houblog, it was on a friend’s site I had admin to… and it’s gone. Rats. I had a great one from a movie on how it was against the university’s policy to make war on the United States. It was so ironic, given the politics of some faculties these days.

  3. Gothmog says:

    Excellent! Thanks so much!

    I’m assuming you use something similar for the other random aspects of your site- instead of quotes is just code to display header/footer/diceroll.
    Am I correct?

  4. I agree with you , your done a great job~

  5. You have done a great job. Thanks for the Lexikon Plugin.

  6. Telcontale says:

    Thanks for the code. I included this code in wordpress footer to get random quotes.

  7. dennis says:

    def gonna use this on a couple of my blogs! thanks a lot mate

  8. Andy Gongea says:

    Can you make it so it uses the same file for the quotes. So instead of using the .txt file, you can store the quotes inside the .php file?

    This will make the file more maintainable and it will use have better performance I think.

    Thanks and kudos.

    1. WJS says:

      That’s a terrible idea. Segregation of code and data is almost always the way to go.

  9. games says:

    I love quotes, espcially motivation ones, they can really change your day :)
    Anyway, nice idea , gonna use it on my blogs also, thanks!

  10. Free Games says:

    Thanks for the handy tip!! That really is an useful piece of information. I have to say I love the way your blog is dressed especially those colored dice thing :)

  11. Oyun says:

    Thanks for the code

  12. Vaidy says:

    Thank you for posting this. Very useful. We’re using this for showing testimonials in the footer our site.

    On another note, I think there’s a small bug in the code – with the offset. We tried the following and it seems to work more consistently:

    function random_phrase () {
    ...
    $num = rand (0, intval (count ($quotes) / 3) - 1) * 3;
    ...
    }

    Hope this helps.

Thanks for joining the discussion. Be nice, don't post angry, and enjoy yourself. This is supposed to be fun. Your email address will not be published. Required fields are marked*

You can enclose spoilers in <strike> tags like so:
<strike>Darth Vader is Luke's father!</strike>

You can make things italics like this:
Can you imagine having Darth Vader as your <i>father</i>?

You can make things bold like this:
I'm <b>very</b> glad Darth Vader isn't my father.

You can make links like this:
I'm reading about <a href="http://en.wikipedia.org/wiki/Darth_Vader">Darth Vader</a> on Wikipedia!

You can quote someone like this:
Darth Vader said <blockquote>Luke, I am your father.</blockquote>

Leave a Reply to WJS Cancel reply

Your email address will not be published.