Created
August 24, 2024 18:32
-
-
Save Qubadi/291ae99b98b11a563df881b906e401e7 to your computer and use it in GitHub Desktop.
Visitor tracker in WordPress with real-time only visible for admins
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Copy the following PHP code and create a PHP snippet using your snippet plugins. | |
Paste the code into the plugin and save it. | |
Visitor tracker in WordPress with real-time updates and a smooth pop-up for admins. | |
___________________________________- | |
// Hook to add the visitor tracking and popup for admins | |
add_action('wp_footer', 'admin_visitor_tracker'); | |
function admin_visitor_tracker() { | |
if (!current_user_can('administrator')) { | |
return; | |
} | |
// Generate a temporary session ID | |
if (!session_id()) { | |
session_start(); | |
} | |
$session_id = session_id(); | |
// Update visitor data | |
$visitors = get_transient('online_visitors') ?: array(); | |
$visitors[$session_id] = time(); | |
set_transient('online_visitors', $visitors, 300); // Store for 5 minutes | |
// Output HTML and JavaScript | |
?> | |
<style> | |
#visitor-popup, #visitor-count-button { | |
position: fixed; | |
right: 20px; | |
background: white; | |
color: #333; | |
padding: 15px; | |
border-radius: 5px; | |
box-shadow: 0 2px 10px rgba(0,0,0,0.1); | |
z-index: 9999; | |
font-size: 14px; | |
font-family: Arial, sans-serif; | |
transition: opacity 0.5s ease-in-out, transform 0.5s ease-in-out; | |
} | |
#visitor-popup { | |
display: none; | |
bottom: 80px; | |
transform: translateY(20px); | |
opacity: 0; | |
} | |
#visitor-count-button { | |
bottom: 20px; | |
cursor: pointer; | |
opacity: 0; | |
transform: translateY(20px); | |
display: none; | |
} | |
#visitor-count-button:hover { | |
background-color: #f0f0f0; | |
} | |
</style> | |
<div id="visitor-popup"> | |
<p>New visitor entered</p> | |
<p>Online visitors: <span id="popup-visitor-count">0</span></p> | |
</div> | |
<div id="visitor-count-button"> | |
Online: <span id="button-visitor-count">0</span> | |
</div> | |
<script> | |
jQuery(document).ready(function($) { | |
var lastCount = 0; | |
var updateAfterPopup = false; // Flag to update the button count after popup | |
function updateVisitorCount() { | |
$.ajax({ | |
url: '<?php echo admin_url('admin-ajax.php'); ?>', | |
type: 'POST', | |
data: { | |
action: 'update_visitor_count', | |
nonce: '<?php echo wp_create_nonce('visitor_tracker_nonce'); ?>' | |
}, | |
success: function(response) { | |
if (response.count > lastCount) { | |
updateAfterPopup = true; // Set flag to update after popup | |
$('#visitor-count-button').animate({opacity: 0, transform: 'translateY(20px)'}, 500, function() { | |
$(this).css('display', 'none'); | |
$('#visitor-popup').css({display: 'block', opacity: 0, transform: 'translateY(20px)'}) | |
.animate({opacity: 1, transform: 'translateY(0)'}, 500) | |
.delay(5000) | |
.animate({opacity: 0, transform: 'translateY(20px)'}, 500, function() { | |
$(this).css('display', 'none'); | |
$('#visitor-count-button').css({display: 'block', opacity: 0, transform: 'translateY(20px)'}) | |
.animate({opacity: 1, transform: 'translateY(0)'}, 500, function() { | |
if (updateAfterPopup) { | |
$('#button-visitor-count').text(response.count); // Update button count after popup is hidden | |
updateAfterPopup = false; // Reset flag | |
} | |
}); | |
}); | |
}); | |
} else { | |
$('#button-visitor-count').text(response.count); // Update button count immediately if no new visitor | |
} | |
$('#popup-visitor-count').text(response.count); // Always update popup count | |
lastCount = response.count; | |
} | |
}); | |
} | |
// Update immediately and then every 5 seconds | |
updateVisitorCount(); | |
setInterval(updateVisitorCount, 5000); | |
// Clear session when leaving the page | |
$(window).on('beforeunload', function() { | |
$.ajax({ | |
url: '<?php echo admin_url('admin-ajax.php'); ?>', | |
type: 'POST', | |
async: false, | |
data: { | |
action: 'remove_visitor', | |
nonce: '<?php echo wp_create_nonce('visitor_tracker_nonce'); ?>' | |
} | |
}); | |
}); | |
}); | |
</script> | |
<?php | |
} | |
// AJAX handler to update visitor count | |
add_action('wp_ajax_update_visitor_count', 'update_visitor_count'); | |
add_action('wp_ajax_nopriv_update_visitor_count', 'update_visitor_count'); | |
function update_visitor_count() { | |
check_ajax_referer('visitor_tracker_nonce', 'nonce'); | |
$visitors = get_transient('online_visitors') ?: array(); | |
$current_time = time(); | |
// Remove inactive visitors | |
foreach ($visitors as $id => $time) { | |
if ($current_time - $time > 300) { | |
unset($visitors[$id]); | |
} | |
} | |
set_transient('online_visitors', $visitors, 300); | |
$count = count($visitors); | |
wp_send_json(array('count' => $count)); | |
} | |
// AJAX handler to remove visitor when leaving | |
add_action('wp_ajax_remove_visitor', 'remove_visitor'); | |
add_action('wp_ajax_nopriv_remove_visitor', 'remove_visitor'); | |
function remove_visitor() { | |
check_ajax_referer('visitor_tracker_nonce', 'nonce'); | |
$session_id = session_id(); | |
$visitors = get_transient('online_visitors') ?: array(); | |
if (isset($visitors[$session_id])) { | |
unset($visitors[$session_id]); | |
set_transient('online_visitors', $visitors, 300); | |
} | |
wp_send_json_success(); | |
} | |
// Track all visitors, not just admins | |
add_action('wp_head', 'track_visitor'); | |
function track_visitor() { | |
if (!session_id()) { | |
session_start(); | |
} | |
$session_id = session_id(); | |
$visitors = get_transient('online_visitors') ?: array(); | |
$visitors[$session_id] = time(); | |
set_transient('online_visitors', $visitors, 300); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment