Further to a shortcode we shared that would render a dynamic countdown with JavaScript, the following code will display a static countdown to an event. Unlike the previous version of the same code where we only counted down to the number of days (previously published on Internoetics), this counter will count down to a specific time. Using WordPress' human_time_diff() function, we'll render human-readable strings to your target time.
The Result
By default, and without any attributes, the shortcode will display the countdown to NYE. For example, [days]
returns: 1 month. Because we're using WP's human readable functions, the rendering isn't overly descriptive. For example, Christmas day in 2030 (using [days date="25th December 2030"]
will show as 6 years. When an expiry has passed (using our last webinar as an example), it will show as passed (7 years ago). The text of 'passed' is determined by a shortcode attribute (I've added del
tags to mine).
To link your result, use the url
attribute. The full date will be returned in the HTML title attribute.
WordPress Shortcode
Copy and paste the WordPress function into your theme's functions.php
file or, if you sensibly have one installed, your custom functions plugin. You may optionally download and install our plugin from the bottom of of the page.
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.
Shortcode Attributes
The following shortcode attributes are available.
date
text
text
is what will be rendered when your time has passed.url
style
style="something: something;"
to style the result.offset
offset
is not defined, we'll use your offset as stored in your WP database. We use offset="10"
(for Sydney).PHP Function
A PHP function to be used outside of WordPress is as follows. Usage requires beliefmedia_human_time_diff().
The offset (in hours) is required when a correction must be applied to your resulting result. To test, set a time one hour in the future and note the difference. The way of determining the offset is a little counter-intuitive (since the intent is manufacture a UTC offset). A negative offset will mean the value will be added to your result.
Considerations
- See also: Display Time To Published or Scheduled Posts (WP Snippet).
Download
Title: Static Day Countdown in WordPress (WP Plugin)
Description: Display a countdown to an event in readable text with shortcode.
Download • Version 0.2, 1.6K, zip, Category: WordPress Plugins (General)
WordPress Shortcodes, (782.0B) PHP Code & Snippets, (743.0B)