Home Blog Display a Random Fact on Your Website with PHP or WordPress Shortcode

Display a Random Fact on Your Website with PHP or WordPress Shortcode

Random Fact

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):

Ernest Vincent Wright wrote a novel, "Gadsby", which contains over 50,000 words -- none of them with the letter E!

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.


Plugin Title: FACTS Author: beliefmedia
Description: Facts is a very simple plugin that’ll generate a random, fascinating, and interesting fact on your…
Download (downloaded 825 times) | Plugin Page

Title: Display a Random Fact on Your Website
Description: Display a Random Fact on Your Website with PHP or WordPress Shortcode.
Download: Shortcode (V0.2) | PHP Code (V0.2) | Plugin Page

■ ■ ■

Interested in WordPress and Web Design? Subscribe to our list and we'll send you subscriber-only tools and plugins

Short URL for this post: http://shor.tt/19nK


Please enter your comment!
Please enter your name here