Select Page
jQuery(document).ready(function($) { // Show the chat icon only for logged-in users if ($('body').hasClass('logged-in')) { $('#chat-icon').show(); } // Toggle chat window visibility $('#chat-icon').click(function() { $('#chat-window').toggle(); }); // Send message when user submits the form $('#send-message').click(function() { var message = $('#chat-input').val(); if (message) { $.ajax({ url: ajaxurl, type: 'POST', data: { action: 'send_chat_message', message: message }, success: function(response) { $('#chat-input').val(''); loadMessages(); } }); } }); // Load messages function loadMessages() { $.ajax({ url: ajaxurl, type: 'POST', data: { action: 'get_chat_messages' }, success: function(response) { $('#chat-messages').html(''); $.each(response.data, function(i, message) { $('#chat-messages').append('

' + message.user_id + ': ' + message.message + '

'); }); } }); } setInterval(loadMessages, 3000); // Refresh chat every 3 seconds });