How To Show Time Spent on Site in WordPress Function

⏲️ 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.


Show Time Spent on WordPress Site

🛠️ 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

Report an issue (max 5 words):

Only logged-in users can submit reports.


Discover more from HelpZone

Subscribe to get the latest posts sent to your email.

Want to support us? Let friends in on the secret and share your favorite post!

Photo of author

Flo

How To Show Time Spent on Site in WordPress Function

Published

Update

Welcome to HelpZone.blog, your go-to hub for expert insights, practical tips, and in-depth guides across technology, lifestyle, business, entertainment, and more! Our team of passionate writers and industry experts is dedicated to bringing you the latest trends, how-to tutorials, and valuable advice to enhance your daily life. Whether you're exploring WordPress tricks, gaming insights, travel hacks, or investment strategies, HelpZone is here to empower you with knowledge. Stay informed, stay inspired because learning never stops! 🚀

👍 Like us on Facebook!

Closing in 10 seconds

Leave a Reply