Are you looking for a way to make a countdown timer shortcode to display content after a specific date? This snippet will make a shortcode that places a countdown timer on specific content within your post. The content will only be visible after the specified date.
Instructions:
- Add this code to your theme’s functions.php file or in a site-specific plugin.
- Add this shortcode to your post with the content you want to display after the specified date. Don’t forget to set the date attributes as well.
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');
[cdt month="11" day="15" year="2020"] This is content that will only be shown after a set number of days. [/cdt]
Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.
If you liked this code snippet, please consider checking out our other articles on the site like: CSS Hero Review: #1 WordPress Theme Customization Plugin.
How to apply this code but to create for Minutos but not Days?
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.
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
Hi Matt I’m sure we can do something like that Ill post a snippet in the near future.
Thanks Kevin 🙂