Adventures in freakdom.
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:
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.
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.
If you want to get notified whenever Fred writes a journal entry, this link will do the trick.
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
| « Feb | ||||||
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 | |||
[...] 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. [...]
[...] You can download and install it yourself, here. [...]
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 http://www.foolishblog.com. Any thoughts?
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!
[...] 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Ã. [...]
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!
Many thanks for this — *exactly* what I was looking for!
Just what I’d been looking for. thanks
does it work on pages also, or could you modify it to work on pages ?
rgds
penge
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.
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
So, could someone help me out on this ?
cheers all
lån penge online - http://www.finai.dk
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?
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.
[...] 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. [...]
Fantastic. What service! Plugin installed, working just fine. Many thanks
http://www.londonkoreanlinks.net/blog/2006/10/10/random-post-plugin/
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.
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″);
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.
Bugger. It took out my code example :p
Not “link text” - but ‘link text’ !!
[...] 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. [...]
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.
Oh, and your “Bad Behavior” plugin isn’t working when I submit a comment here. That’s more of an FYI for you.
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.
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.
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’”. )
[...] 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 [...]
[...] El plugin de wordpress os lo podeis descargar desde aqui Leer entradas relacionadas: [...]
Thanks for this plugin.
[...] Visit [...]
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!
[...] 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. [...]
please update your instructions example: .
[...] Basta utilizar o plugin “Random post link” o download e as instruções você vê lá. [...]
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.
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>
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.
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!
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!
Hello over there.
Just thought I would report in on the latest place your plugin is installed and working just great.
In fact I added an image instead of text and I can’t stop clicking it myself now !
You’ll see it on the sidebar of my site - the “Runaround” animated image.
(Wordpress 2.6.5 installation. aok)
The suggestions from peterpoe on permalinks should be included into the plugin - maybe with a switch/option.
Thanks for a small but nice plugin.
What happened to the chickens? HAHA
Hello, thanks for the effort on maintaining this plugin with the first update in 3 years
I have a suggestion for you, could you slightly update the php code that we can insert into our blog (for instance in the header) so that it can work with something like or any working solution ?
The advantage of a formulation like that would be that we could insert the random link function into our header, without having a broken header as soon as we deactivate the plugin…