X

Countdown timer shortcode, display content after specified date

Responsive Review - customize home page

Adding the first snippet to the functions.php of your wordpress theme will let you use a shortcode to place a countdown timer on specific content within your post. Add the shortcode within the second snippet to your post with content you want to display after the specified date. I added a class of event so you can style the display of the message anyway you wish, enjoy!



Code Snippet:


function content_countdown($atts, $content = null){
extract(shortcode_atts(array(
'month' => '',
'day' => '',
'year' => ''
), $atts));
$remain = ceil((mktime( 0,0,0,(int)$month,(int)$day,(int)$year) – time())/86400);
if( $remain > 1 ){
return $daysremain = "<div class=\"event\">Just <b>($remain)</b> days until content is available</div>";
}else if($remain == 1 ){
return $daysremain = "<div class=\"event\">Just <b>($remain)</b> day until content is available</div>";
}else{
return $content;
}
}
add_shortcode('cdt', 'content_countdown');


Code Snippet:


[cdt month="10" day="17" year="2011"]
This is content that will only be shown after a set number of days.
[/cdt]

Comments  Leave a Reply

  1. wickandcoilchurch August 15, 2015 at 2:36 pm

    How can we make this loop as I wish to have a button at the end instead of text. Once the button is clicked it does said action related to the button and simply shows timer again. If user is not logged in it shall simply show the button until he/she logs in and clicks on it.

  2. Hey,

    Great piece of code 🙂

    Is there a way to remove the content on a set date? So you can set the date to show the content and it can expire on a set date?

    MAtt

    1. Hi Matt I’m sure we can do something like that Ill post a snippet in the near future.

      1. Thanks Kevin 🙂

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!