vituperation

Adventures in freakdom.

Random post link plugin

by @ 3:07 pm on September 7, 2005.

As a new WordPress user, I’ve been fascinated by the concept of plugins to extend the software’s functionality. Because I’m a geek by nature, and a software writer by profession, I couldn’t wait to try my hand at writing my own plugin.

One thing I’ve always thought would be nifty is a sidebar link to take a visitor to a random post on my site, so I decided to try that as my first WordPress plugin. The random post link plugin is about as simple as a plugin can be: it’s a single PHP function, called fla_random_post(). You can see it in action in the right sidebar, where it says “Get me a random entry!”

To install the plugin, download it from this link and gunzip the PHP source file (random_post.php) into your wp-content/plugins directory. Activate the plugin on the “Plugins” page of your WordPress admin panel, and you’re ready to go.

Once you’ve activated the plugin, you can get a random post link by calling the fla_random_post() function wherever you want in your template. 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 that fully qualified URL to you for you to use. It displays nothing.

39 Responses to “Random post link plugin”
  1. WordPress Station » Blog Archive » Random post plugin said:

    […] vituperation.com Has released a plugin that can provide a link to a random post. It doesn’t seem to actually show the post, just a link to it. […]

  2. Bitchypoo » 9/8/05 said:

    […] You can download and install it yourself, here. […]

  3. Eric Zeller said:

    Fred - great idea… But it isn’t working for me. I have added a link on my sidebar using , but it seems to just be a link to whatever page I am currently on in the site. You can see this at www.foolishblog.com. Any thoughts?

  4. Fred said:

    Eric, I can see what you’re saying (about your site). I’d like to email you to get a little more information, but you didn’t leave an address here and the ‘Ask a question’ link on your site gives PHP errors in a spell_check plugin. Can you email me (address on my contact page or in plugin) and send me the section of your sidebar template where you’re making the call to the plugin?

    thanks!

  5. La PDA de tungsteno » Archivo del weblog » Novedades en el blog said:

    […] Gracias al plugin Random post, ahora debajo del título del blog tenéis un enlace que se llama elegir página al azar. Es una manera muy divertida de descubrir nuevas cosas escritas en este blog. Al hacer clic iréis directamente a un post al azar del blog, y por supuesto en la cabecera seguirá el enlace, esperando que lo volváis a usar. Esto permite ir navegando a través del contenido del blog de manera aleatoria y muy fácilmente. Os recomiendo probarlo una rato, porque con el paso del tiempo ya se ha acumulado bastante contenido para leer, y hasta a mí me ha sorprendido redescubrir cosas que hace bastante tiempo que están aquí. […]

  6. sindhu said:

    uhm need help! i put something like this on my blog sidebar.php file and nothing is showing up in the box ..

    confused! please help!

  7. Greg said:

    Many thanks for this — *exactly* what I was looking for!

  8. Fence said:

    Just what I’d been looking for. thanks

  9. penge på nettet said:

    does it work on pages also, or could you modify it to work on pages ?

    rgds
    penge

  10. Fred said:

    Penge:

    Right now it doesn’t, but you could pretty easily modify the PHP to do that. Without actually trying it myself, I’d suspect it’s just a matter of changing the ‘SELECT’ statement to fetch from the POSTS table where POST_STATUS is ’static’ instead of ‘publish’.

    I can’t guarantee that will work, but some playing around should get you where you want to be. :)

  11. penge på nettet said:

    i’m a real nob on php, so it’ll take me ages to figure that simple instructions out :) can u test it for me then ?

    rgds
    penge

  12. www.lån.dk said:

    So, could someone help me out on this ?

    cheers all
    lån penge online - http://www.finai.dk

  13. Philip said:

    Sorry to be completely useless, but I’ve downloaded the file (only 2k?), tried every which way at gzip.org to download and get something which works on my basic Windows XP system, but nowhere can I find anything which will gunzip your file. How about a simple Zip version?

  14. Fred said:

    Philip:

    You should be able to download a text version of the plugin here. Just right-click the link, pick “Save as” or “Save link as”, and save it somewhere. Remember to take the .txt file extension off when you use the plugin.

  15. London Korean Links / Random Post plugin said:

    […] Thanks to Fred Anderson over at Vituperation for an ingenious little gizmo called the Random Post plugin. You’ll see it at the top of the sidebar, in between the search box and the list of categories. So if you’re feeling lucky, just click on the link and, as you might expect, you’ll get a random post from my site. […]

  16. Philip said:

    Fantastic. What service! Plugin installed, working just fine. Many thanks
    http://www.londonkoreanlinks.net/blog/2006/10/10/random-post-plugin/

  17. Llynix said:

    Nice, just installed to my website without any big snags. You may want to note that if copying and pasting from your instructions quotes may get mangled.

    Any way to get only posts from a certain category? Or to exclude posts from a category? I looked at the simple code, but am not familiar enough with wordpress’ tables to hack away (category_id didn’t seem to work).

    Thanks for the plugin.

  18. Llynix said:

    Got it to work!!! Here’s the hack. You’ll want to change the category_id = 1 to whatever number the category you want is. Also you have to change id to post_id later on in the code.

    $row = $wpdb->get_row(”select post_id from $wpdb->post2cat,$wpdb->posts”.
    ” where $wpdb->post2cat.category_id = 1″.
    ” and ($wpdb->posts.ID = $wpdb->post2cat.post_id”.
    ” and $wpdb->posts.post_status = ‘publish’)”.
    ” order by rand() limit 1″);

  19. cloudhopper said:

    Oh dear.

    I’ve been going crazy trying to get this going - I’m just an “out of the box” bloke, and don’t really understand what is going on with php ;o)

    I liked this little plugin, couldn’t get it to work, tried lots of others and found nothing as slick or as simple, nor that did *exactly* what I wanted :o (

    So I came back. And persevered. Over coffee.

    And finally:

    That’s *single quotes* … not double ones!

    Sweet!! Thanks, Fred.

  20. cloudhopper said:

    Bugger. It took out my code example :p

    Not “link text” - but ‘link text’ !!

  21. Mais uma novidade: Caixa de Pandora | novo-MUNDO - Mundo feito de internet, curiosidades e blogs said:

    […] Subproduto de toda esta abobrinha que eu disse é a nova funcionalidade no blog: a Caixa de Pandora. Clique em “Disparar” e você irá parar em um local qualquer do novo-MUNDO. Algo como aquele teletransporte quebrado do mochileiros. Usei o Plugin Random Post. […]

  22. Jim said:

    With the new version 2.1 of Wordpress, the Random Post plugin will attempt to display PAGES as well as POSTS, but when the random entry selected is a page the result is a 404 error.

  23. Jim said:

    Oh, and your “Bad Behavior” plugin isn’t working when I submit a comment here. That’s more of an FYI for you.

  24. Moridin said:

    I like this plugin a lot. However, I use custom permalinks on my blog, where as the link outputs the ?=id version of the URL. If you ever modify this plugin, this would be a feature request by the people who use custom permalinks.

  25. peterpoe said:

    Jim:

    Didn’t encounter the 404 error. To make the plugin only use posts, add this line:

    “and post_type=’post’”.

    after line 61 ( “where post_status=’publish’”. )

    Moridin:

    I modified the plugin to use permalinks. Change the if($row) section with this:

    if ($row) {
    $random_permalink = get_permalink($row->id);
    if (strlen($text)) {
    $random = “<a href=\”$random_permalink\”>$text”;
    echo $random;
    }
    else $random = “$random_permalink”;
    }
    else $random = “”;

    return $random;
    }

    Hope this helped.

  26. Oliver said:

    Very interesting plugin, really, thank you a lot !

    And Peterpoe, I thank you too, because you gave me the solution to the problem of the plugin failing because he was tried to open pages :)

    A small remark, the blog’s text managing seems to have transformed the ” and ‘ characters, making them different from the ones that would work in the php code on my computer o_O
    The ” for instance isn’t vertical but slightly bended, if you see the idea, and it needs the vertical two lines ” to work in php.

    Perhaps a problem with localization of special characters in your country or in mine, but I was saying this in case people tried your edit and thought it didn’t work. It works ! Just enter yourself the ” and ‘ characters :)

    (and to see if it’s linked to that blog’s text managing or to my computer, lastly I paste the code that worked for me, if it’s still bugged, so that’s linked to vituperation.com
    ” and post_type=’post’”. )

  27. Blog About Your Blog said:

    […] This little piece of code puts a link in your sidebar to a random post in your archive.  This way if a reader clicks on it they’ll be treated to random look into your sordid blogging past.  Random Post Plugin […]

  28. » Post al Azar » Blog Archive El diario sin diario said:

    […] El plugin de wordpress os lo podeis descargar desde aqui  Leer entradas relacionadas: […]

  29. Webkoktejl said:

    Thanks for this plugin.

  30. Dominic said:

    Excellent plugin - couldn’t get it to work calling the function without text, and I had issues because my blog loads from my root directory (like yours), but random_post.php was trying to throw an extra /wordpress/ in the URL. Anyway, your plugin works great once I figured out what I was doing wrong! Thanks!

  31. Random WordPress Plugins: Rotating Banners, Header Art, Images, Quotes, and Content on Your Blog « Lorelle on WordPress said:

    […] Random Post Link Plugin puts a button or link on your sidebar that, when clicked, takes the visitor to a random post on your blog. […]

  32. thesuperstar said:

    please update your instructions example: .

  33. Post randômico no Wordpress « Info Bits said:

    […] Basta utilizar o plugin “Random post link” o download e as instruções você vê lá. […]

  34. michal said:

    It’s very nice plugin, but I have one question - is it possible to display the title of the random published post somehow instead of “link text”? I’ve tried to make WP do it but I don’t understand PHP so much… Thank you.

  35. Marilyn said:

    I have the same question as michal above. I tried a few combinations without success any ideas? The best I could think of was -

    <a href=”<?php fla_random_post(); ?>”><?php the_title(); ?></a>

  36. Jon-Jon said:

    Very, very good. It installed and started working smoothly. Great job and thank you very much for creating this. I’ll pass on the word about your great work. Running WordPress 2.5-RC2 and it works flawlessly.

  37. Fact Frenzy said:

    Ok I upgraded to the full WP 2.5 (not the beta version). It works great without a hitch.
    A must-have plugin for any blog. Thanks!

  38. Tim said:

    Sounds like a fantastic plugin! Is there a way to make it go to a random post within the SAME category that you are currently viewing? For example, I have a flashcard website for students covering several subjects. I’d like to have a link within my theme that links to a random question within the SAME category that the student is already viewing.

    Thanks!

Leave a Reply

vi·tu·per·a·tion n. Sustained and bitter railing and condemnation: vituperative utterance

navigation:

subscribe:

If you want to get notified whenever Fred writes a journal entry, this link will do the trick.

reading:





in the world:

Copyright

© 2002-2008 vituperation.com
All rights reserved. Please don't steal.

online:

12 people on
1802004 since 8/31/05


curious:

Get me a random entry!

gratuitous ads:

categories:

search vituperation:


archives:

October 2008
S M T W T F S
« Sep    
 1234
567891011
12131415161718
19202122232425
262728293031  
(all archives)

current poll:

Where would you rather live?

View Results