Voxfor - All rights reserved - 2013-2025
We Accepted





Real-time notifications keep WordPress admins and users informed the moment important events happenโlike new orders, comments, or security alerts. They eliminate the need for manual page refreshing, saving time and improving user experience. Whether you’re running an online store, blog, or membership site, instant updates help you act faster and keep your audience engaged.
In this guide, you’ll learn two methods to add real-time notifications to your WordPress admin panel.
We’ll provide code examples, explain key concepts, and share tips for security and performance. Let’s dive in!
AJAX polling checks the server for updates at regular intervals. It’s easy to set up but slightly less efficient than WebSockets.
The code needs to be incorporated into functions.php within your theme or a custom plugin.
// Step 1: Register a REST API endpoint for notifications
add_action('rest_api_init', 'register_custom_notifications_route');
function register_custom_notifications_route() {
register_rest_route(
'notify/v1',
'/latest',
array(
'methods' => 'GET',
'callback' => 'get_latest_notifications',
'permission_callback' => 'verify_user_access'
)
);
}
// Step 2: Fetch notifications (example: recent comments)
function get_latest_notifications() {
$args = array(
'number' => 5, // Get 5 latest comments
'status' => 'approve'
);
$comments = get_comments($args);
return new WP_REST_Response($comments, 200);
}
// Step 3: Security check (only admins can access)
function verify_user_access() {
return current_user_can('edit_posts');
}
What This Code Does:
Add this script to your admin dashboard (use admin_enqueue_scripts to load it):
// Check for new notifications every 30 seconds
document.addEventListener('DOMContentLoaded', function() {
const notificationList = document.getElementById('notification-list');
function fetchNotifications() {
fetch('/wp-json/notify/v1/latest')
.then(response => response.json())
.then(data => {
if (data.length > 0) {
updateNotificationList(data);
}
})
.catch(error => console.error('Error:', error));
}
function updateNotificationList(notifications) {
notificationList.innerHTML = notifications
.map(comment => `
<div class="notification-item">
<p>New comment by ${comment.author_name}:</p>
<p>${comment.content}</p>
</div>
`).join('');
}
// Initial fetch + repeat every 30 seconds
fetchNotifications();
setInterval(fetchNotifications, 30000);
});
What This Code Does:
WebSockets enable true real-time communication by maintaining an open connection between the server and the client. We’ll use Pusher, a third-party service, to simplify setup.
Install the Pusher PHP SDK using Composer:
composer require pusher/pusher-php-server
Add this code to functions.php:
use Pusher\Pusher;
// Initialize Pusher
function setup_pusher() {
$options = [
'cluster' => 'YOUR_CLUSTER',
'useTLS' => true
];
return new Pusher(
'YOUR_APP_KEY',
'YOUR_APP_SECRET',
'YOUR_APP_ID',
$options
);
}
// Example: Send a notification on new user registration
add_action('user_register', 'send_new_user_notification');
function send_new_user_notification($user_id) {
$user = get_userdata($user_id);
$pusher = setup_pusher();
$pusher->trigger('admin-notifications', 'new-user', [
'message' => "New user registered: " . $user->user_email,
'time' => current_time('mysql')
]);
}
What This Code Does:
Add this JavaScript to your admin area:
// Load Pusher from CDN
<script src="https://js.pusher.com/7.0/pusher.min.js"></script>
// Initialize and subscribe to the channel
const pusher = new Pusher('YOUR_APP_KEY', {
cluster: 'YOUR_CLUSTER'
});
const channel = pusher.subscribe('admin-notifications');
// Listen for 'new-user' events
channel.bind('new-user', function(data) {
const notificationArea = document.getElementById('real-time-notifications');
notificationArea.innerHTML += `
<div class="alert alert-info">
<strong>${data.time}</strong>: ${data.message}
</div>
`;
});
What This Code Does:
// In your REST endpoint registration
'permission_callback' => function() {
return wp_verify_nonce($_REQUEST['nonce'], 'your_nonce_action');
}
// Cache comments for 1 minute
set_transient('cached_comments', $comments, 60);
Real-time notifications turn your WordPress admin into a dynamic, responsive hub where critical updatesโlike new orders, user registrations, or commentsโare delivered instantly. By choosing AJAX polling, you gain a beginner-friendly solution that’s simple to implement and ideal for smaller sites or those prioritizing ease of setup. For more advanced needs, WebSockets (powered by services like Pusher) offer true real-time communication, ensuring zero delays for high-traffic platforms or time-sensitive workflows. Both methods empower you to act faster, reduce manual checks, and create a seamless experience for admins and users alike.
As you implement these solutions, remember that security and performance are key. Validate user permissions, sanitize data, and leverage caching to keep your site running smoothly. The future of WordPress management lies in automation and real-time interaction, and by integrating notifications, you’re staying ahead of the curve. Explore the Word REST API Handbook or Pusher’s guides to dive deeper and start transforming how your team interacts with your siteโone alert at a time.
Hassan Tahir wrote this article, drawing on his experience to clarify WordPress concepts and enhance developer understanding. Through his work, he aims to help both beginners and professionals refine their skills and tackle WordPress projects with greater confidence.