⏲️ Estimated reading time: 2 min
Here’s a simple WordPress function that Show Time, how long a visitor has been on your site. It uses JavaScript to track the time and displays it anywhere on your site using a shortcode.
🕒 Show Time Spent on Site in WordPress Function
Display how long a visitor has stayed on your WordPress site with a simple shortcode. Great for engagement stats or fun widgets.
📌 Step-by-step: Show Visitor Time on Site
1. Add this to your WordPress Function php
file (or use a custom plugin):
function show_time_on_site() {
ob_start(); ?>
<div id="time-on-site">Time on site: <span id="timer">0</span> seconds</div>
<script>
let seconds = 0;
setInterval(() => {
seconds++;
document.getElementById('timer').textContent = seconds;
}, 1000);
</script>
<?php
return ob_get_clean();
}
add_shortcode('time_on_site', 'show_time_on_site');
2. Use the shortcode in any post or page:
[time_on_site]
This will render a live counter showing how long the visitor has been on the page.

🛠️ Optional Enhancements WordPress Function
- Time in minutes and seconds.
- Log time to analytics.
- Style with CSS for better visuals.
🏷️ Tags: wordpress, functions.php, shortcode, time tracker, user engagement, javascript, site stats, wp tips, website timer, visitor duration
Only logged-in users can submit reports.
Discover more from HelpZone
Subscribe to get the latest posts sent to your email.