{"id":46360,"date":"2025-03-02T20:00:39","date_gmt":"2025-03-02T20:00:39","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/?p=46360"},"modified":"2026-10-10T21:00:16","modified_gmt":"2026-10-10T21:00:16","slug":"countdown-time-with-tailwind-css-and-javascript","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/countdown-time-with-tailwind-css-and-javascript\/","title":{"rendered":"WordPress Countdown Timer Shortcode With Tailwind CSS"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A WordPress countdown timer for a launch, a sale or an event usually comes from a heavy plugin. This shortcode prints days, hours, minutes and seconds until any date, counts down live in the browser and swaps in your own message when the time is up.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The older version of this post hard-coded the date in JavaScript, used fixed element IDs so only one timer could work per page, and threw errors on every page without the timer. Now the date is a shortcode attribute in your site&#8217;s timezone, and you can place as many timers on a page as you like:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>&#91;dpc_countdown date=\"2026-12-31 18:00\"&#93;<\/code><\/li>\n\n\n<li><code>&#91;dpc_countdown date=\"2026-11-27 09:00\" expired=\"Black Friday is over.\"&#93;<\/code><\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">The snippet<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\/**\n * &#91;dpc_countdown date=\"2026-12-31 18:00\" expired=\"The offer has ended.\"&#93;\n * Days, hours, minutes and seconds until a date in the site's timezone\n * (Settings \u2192 General). Styled with Tailwind classes; readable without them.\n *\/\nadd_shortcode( 'dpc_countdown', 'dpc_countdown_shortcode' );\n\nfunction dpc_countdown_shortcode( $atts ): string {\n\t$atts = shortcode_atts(\n\t\tarray(\n\t\t\t'date'    =&gt; '',\n\t\t\t'expired' =&gt; __( 'This countdown has ended.', 'dpc' ),\n\t\t),\n\t\t$atts,\n\t\t'dpc_countdown'\n\t);\n\n\t$end = '' !== trim( $atts&#91;'date'&#93; ) ? date_create_immutable( $atts&#91;'date'&#93;, wp_timezone() ) : false;\n\tif ( ! $end ) {\n\t\t\/\/ Only people who can edit content see why nothing rendered.\n\t\treturn current_user_can( 'edit_posts' )\n\t\t\t? '&lt;p class=\"dpc-countdown-error\"&gt;' . esc_html__( 'Countdown: set date=\"YYYY-MM-DD HH:MM\".', 'dpc' ) . '&lt;\/p&gt;'\n\t\t\t: '';\n\t}\n\n\t$left = $end-&gt;getTimestamp() - time();\n\tif ( $left &lt;= 0 ) {\n\t\treturn '&lt;p class=\"dpc-countdown dpc-countdown--expired\"&gt;' . esc_html( $atts&#91;'expired'&#93; ) . '&lt;\/p&gt;';\n\t}\n\n\tdpc_countdown_script();\n\n\t\/\/ Start values from the server, so the timer is correct before the script runs.\n\t$units = array(\n\t\t'days'    =&gt; array( __( 'Days', 'dpc' ), intdiv( $left, DAY_IN_SECONDS ) ),\n\t\t'hours'   =&gt; array( __( 'Hours', 'dpc' ), intdiv( $left % DAY_IN_SECONDS, HOUR_IN_SECONDS ) ),\n\t\t'minutes' =&gt; array( __( 'Min.', 'dpc' ), intdiv( $left % HOUR_IN_SECONDS, MINUTE_IN_SECONDS ) ),\n\t\t'seconds' =&gt; array( __( 'Sec.', 'dpc' ), $left % MINUTE_IN_SECONDS ),\n\t);\n\n\t$boxes = '';\n\tforeach ( $units as $unit =&gt; list( $label, $value ) ) {\n\t\t$boxes .= sprintf(\n\t\t\t'&lt;div class=\"dpc-countdown__unit flex flex-col items-center bg-amber-200 p-4\"&gt;&lt;span class=\"dpc-countdown__value text-5xl\" data-unit=\"%1$s\"&gt;%2$s&lt;\/span&gt; &lt;span class=\"dpc-countdown__label\"&gt;%3$s&lt;\/span&gt;&lt;\/div&gt;',\n\t\t\tesc_attr( $unit ),\n\t\t\tesc_html( 'days' === $unit ? (string) $value : sprintf( '%02d', $value ) ),\n\t\t\tesc_html( $label )\n\t\t);\n\t}\n\n\treturn sprintf(\n\t\t'&lt;div class=\"dpc-countdown flex flex-wrap gap-4 justify-center items-center\" role=\"timer\" aria-label=\"%1$s\" data-dpc-countdown=\"%2$d\" data-dpc-expired=\"%3$s\"&gt;%4$s&lt;\/div&gt;',\n\t\t\/* translators: %s: end date and time. *\/\n\t\tesc_attr( sprintf( __( 'Countdown to %s', 'dpc' ), wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $end-&gt;getTimestamp() ) ) ),\n\t\t$end-&gt;getTimestamp(),\n\t\tesc_attr( $atts&#91;'expired'&#93; ),\n\t\t$boxes\n\t);\n}\n\n\/** One footer script per page, and only on pages that show a countdown. *\/\nfunction dpc_countdown_script(): void {\n\tif ( wp_script_is( 'dpc-countdown', 'enqueued' ) ) {\n\t\treturn;\n\t}\n\n\t$js = &lt;&lt;&lt;'JS'\n(function () {\n\tfunction pad(n) {\n\t\treturn n &lt; 10 ? '0' + n : String(n);\n\t}\n\n\tfunction start(box) {\n\t\tvar end = parseInt(box.getAttribute('data-dpc-countdown'), 10) * 1000;\n\t\tvar fields = {};\n\t\tbox.querySelectorAll('&#91;data-unit&#93;').forEach(function (el) {\n\t\t\tfields&#91;el.getAttribute('data-unit')&#93; = el;\n\t\t});\n\t\tvar timer;\n\n\t\tfunction tick() {\n\t\t\tvar left = Math.floor((end - Date.now()) \/ 1000);\n\t\t\tif (isNaN(left) || left &lt;= 0) {\n\t\t\t\tclearInterval(timer);\n\t\t\t\tvar done = document.createElement('p');\n\t\t\t\tdone.className = 'dpc-countdown dpc-countdown--expired';\n\t\t\t\tdone.textContent = box.getAttribute('data-dpc-expired') || '';\n\t\t\t\tbox.replaceWith(done);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tfields.days.textContent = Math.floor(left \/ 86400);\n\t\t\tfields.hours.textContent = pad(Math.floor((left % 86400) \/ 3600));\n\t\t\tfields.minutes.textContent = pad(Math.floor((left % 3600) \/ 60));\n\t\t\tfields.seconds.textContent = pad(left % 60);\n\t\t}\n\n\t\ttick();\n\t\ttimer = setInterval(tick, 1000);\n\t}\n\n\tfunction init() {\n\t\tdocument.querySelectorAll('&#91;data-dpc-countdown&#93;').forEach(start);\n\t}\n\n\tif (document.readyState === 'loading') {\n\t\tdocument.addEventListener('DOMContentLoaded', init);\n\t} else {\n\t\tinit();\n\t}\n})();\nJS;\n\n\twp_register_script( 'dpc-countdown', false, array(), null, array( 'in_footer' =&gt; true ) );\n\twp_add_inline_script( 'dpc-countdown', $js );\n\twp_enqueue_script( 'dpc-countdown' );\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Add it with Scripts Organizer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/dplugins.com\/downloads\/scripts-organizer\/\">Scripts Organizer<\/a> go to <strong>Code Blocks \u2192 Add New<\/strong> and give the block a name. Set <strong>Trigger location<\/strong> to <strong>Everywhere<\/strong>, tick <strong>PHP<\/strong> under <strong>Script location<\/strong>, paste the code into the PHP editor and publish. Leave <strong>Action\/Hook<\/strong> empty. Then put <code>&#91;dpc_countdown date=\"\u2026\"&#93;<\/code> in a Shortcode block, or in any page builder&#8217;s shortcode element.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is a normal <code>add_shortcode()<\/code> rather than a block with the <strong>Shortcode<\/strong> script location because every timer needs its own date and message, and a Shortcode-location block cannot read attributes. The script is printed by the shortcode itself, once per page and only where a timer is shown, so there is no separate Footer part to set conditions for. The same code works unchanged in a small plugin or an mu-plugin.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The look comes from Tailwind CSS classes (<code>flex<\/code>, <code>gap-4<\/code>, <code>bg-amber-200<\/code>, <code>p-4<\/code>, <code>text-5xl<\/code>), compiled for example by <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/dplugins.com\/downloads\/winden\/\">Winden<\/a>. Tailwind is only needed for that styling: without it the timer still counts down and reads as four plain lines such as \u201c2 Days\u201d, \u201c03 Hours\u201d.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How it works<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>date_create_immutable()<\/code> reads the <code>date<\/code> attribute in the timezone from <strong>Settings \u2192 General<\/strong>, so <code>18:00<\/code> means 18:00 on your site, not on the visitor&#8217;s computer. An invalid date renders nothing for visitors and a short notice for editors.<\/li>\n\n\n<li>The shortcode calculates the remaining time on the server and prints it into the markup. The timer is right on first paint, and still shows sensible numbers without JavaScript.<\/li>\n\n\n<li>The end time travels as a Unix timestamp in <code>data-dpc-countdown<\/code>. The script starts one interval per timer, writes the numbers with <code>textContent<\/code>, and replaces the timer with the <code>expired<\/code> text when it reaches zero.<\/li>\n\n\n<li>A date in the past prints only the escaped <code>expired<\/code> message. All attributes are escaped, and <code>role=\"timer\"<\/code> with an <code>aria-label<\/code> tells screen readers what the numbers are without announcing every second.<\/li>\n\n\n<li>The script is registered as a handle without a file and added once in the footer, however many timers the page has.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Customise it<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Colours and sizes: change the Tailwind classes in the two <code>sprintf()<\/code> templates, for example <code>bg-amber-200<\/code> to <code>bg-violet-100<\/code> or <code>text-5xl<\/code> to <code>text-4xl md:text-6xl<\/code>.<\/li>\n\n\n<li>Labels: edit <code>Days<\/code>, <code>Hours<\/code>, <code>Min.<\/code> and <code>Sec.<\/code> in <code>$units<\/code>; they are translatable with the <code>dpc<\/code> text domain.<\/li>\n\n\n<li>Hide the days box for short timers: remove the <code>days<\/code> row and add its value to the hours.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Gotchas<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Full-page caching stores the start values. The script corrects them as soon as it runs, so a cached page may show a jump of a few seconds or minutes.<\/li>\n\n\n<li>After the end date a cached page keeps showing the timer until the cache is cleared; the script still swaps in the expired message.<\/li>\n\n\n<li>Tailwind compilers only generate classes they can find. If the boxes come out unstyled, add the classes to your Tailwind safelist.<\/li>\n\n<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>A WordPress countdown timer shortcode: days, hours, minutes and seconds to a date in your site&#8217;s timezone, styled with Tailwind CSS classes.<\/p>\n","protected":false},"author":1,"featured_media":46882,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"WordPress Countdown Timer Shortcode With Tailwind CSS","description":"A WordPress countdown timer shortcode: days, hours, minutes and seconds to a date in your site's timezone, styled with Tailwind CSS classes."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[3],"tags":[],"dpc_library":[204],"class_list":["post-46360","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/comments?post=46360"}],"version-history":[{"count":2,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46360\/revisions"}],"predecessor-version":[{"id":46881,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46360\/revisions\/46881"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46882"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=46360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/categories?post=46360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=46360"},{"taxonomy":"dpc_library","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/dpc_library?post=46360"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}