This article will show you how to display a random fact on your website using PHP or WordPress shortcode. Not unlike our article on displaying a random quote (which is actually quite decent) and another (scheduled) article on displaying a random insult, they were created simply to make use of our suite of content APIs. In an attempt to establish a presence in the WordPress repository, we submitted the Insult plugin making it, quite possibly, the crappiest plugin WordPress has ever seen. That said, it's found a limited audience and we'll continue to support it.
In migrating our services over to our new website we'll be updating all the APIs to use JSON data almost exclusively.
Most of the facts that we have on record are interesting and valid. Others are crap and we have no idea if they're true. Some of the records are submitted via a few different third-party platforms so we don't have an overly effective way of validating all the submitted data.
The Result
The result of the [fact]
shortcode will render something like this (bolding and blockquote is mine):
George Washington and Thomas Jefferson grew marijuana on their plantations!
True? No idea.
The Code
Copy and paste the WordPress function into your theme's functions.php
file or, if you sensibly have one installed, your custom functions plugin.
If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.
The cache period can be defined in your shortcode as cache="3600"
(one hour), or hard-coded into your shortcode function.
PHP Function
Used outside of WordPress the following function can be used. It requires that you include Simple Cache.
Download
Title: Display a Random Fact on Your Website
Description: Display a Random Fact on Your Website with PHP or WordPress Shortcode.
Download • Version 0.2, 668.0B, zip, Category: PHP Code & Snippets
WordPress Shortcodes, (675.0B)