Ecommerce Shopify WordPress Discussion

Logout and redirect WordPress user id without he refresh the page

I m developing a functionality triggered on a button $('.disconnect-classroom-button') when click, I logout all members inside that group, and the logout is done successfully ( but the user has to click or refresh the page to take effect even I use ajax ), this is not the result I want : here is the php callback functions : // PHP function to send students_ids function send_students_ids() { if (isset($_POST['payload']) && $_POST['payload'] == 'disconnect_classroom') { // Verify nonce check_ajax_referer('kwf_nonce', 'security'); $group_type = bp_groups_get_group_type(bp_get_current_group_id()); $students_ids = array(); $group_members = groups_get_group_members(array( 'group_id' => bp_get_current_group_id(), 'exclude_admins_mods' => true, 'exclude_banned' => true, 'exclude' => false, 'group_role' => array('member'), 'search_terms' => false, )); foreach ($group_members['members'] as $member) { $students_ids[] = $member->ID; } // Send students_ids to the JavaScript function wp_send_json_success(array('students_ids' => $students_ids)); } } // Add the custom action hook to the wp_ajax_ hook add_action('wp_ajax_send_students_ids', 'send_students_ids'); // PHP function to process logout using students_ids function kwf_ajax_logout_group() { if (isset($_POST['payload']) && $_POST['payload'] == 'disconnect_classroom') { // Verify nonce check_ajax_referer('kwf_nonce', 'security'); // Get the user IDs from the POST data $user_ids = $_POST['user_ids']; // Loop through the user IDs and log them out foreach ($user_ids as $user_id) { if (is_user_logged_in($user_id)) { // Get all sessions for the user with ID $user_id $sessions = WP_Session_Tokens::get_instance($user_id); // We have got the sessions, destroy them all! $sessions->destroy_all(); } } // Send a success response wp_send_json_success(array( 'message' => 'You will be logged out.', 'user_ids' => $user_ids, 'redirect_url' => home_url(), // Set the redirection URL )); // Redirect to the homepage after logging out // exit; } } and here is the event handler in ajax : ( enqueued only for single group pages ): if(function_exists('bp_is_groups_component') && bp_is_groups_component()) { // single group wp_enqueue_script( 'kwf-bp-groups-js', KWF_JS_PATH . 'bp-groups.js', array( 'jquery' ), KWF_THEME_VERSION, true );} $('.disconnect-classroom-button').hover( function () { // Hover in: Change the icon to bb-icon-file-export with ease-in-out transition $(this).find('i').removeClass('bb-icon-file-import').addClass('bb-icon-file-export').css('transition', 'all 0.3s ease-in-out'); }, function () { // Hover out: Change the icon back to bb-icon-file-import with ease-in-out transition $(this).find('i').removeClass('bb-icon-file-export').addClass('bb-icon-file-import').css('transition', 'all 0.3s ease-in-out'); } ).on('click', function (e) { e.preventDefault(); var button = $(this); var nonce = $(this).data('security'); NProgress.start(); NProgress.set(0.4); // Increment var interval = setInterval(function () { NProgress.inc(); }, 1000); // Step 1: Send Member IDs $.ajax({ type: 'post', dataType: 'json', url: ajaxurl, data: { action: 'send_students_ids', payload: 'disconnect_classroom', security: nonce, send_members: true, // Add a flag to indicate sending member IDs }, success: function (response) { // Check if sending member IDs was successful if (response.success) { var memberIds = response.data.students_ids; // Step 2: Process Logout $.ajax({ type: 'post', dataType: 'json', url: ajaxurl, headers: { pragma: "no-cache", "cache-control": "no-cache" }, data: { action: 'kwf_ajax_logout_group', payload: 'disconnect_classroom', security: nonce, user_ids: memberIds, // Include the member IDs for logout }, success: function (logoutResponse) { // Check if logout was successful if (logoutResponse.success) { button.text(kwf_translate.disconnected_classroom); NProgress.done(); clearInterval(interval); // Step 3: Send Success Message jQuery(document).trigger( 'bb_trigger_toast_message', [ '', '<div>' + kwf_translate.disconnected_message + '</div>', 'info', null, true ] ); // Trigger custom event for successful logout console.log(logoutResponse.data) // Dispatch a custom event after successful logout var logoutEvent = new CustomEvent('logoutSuccess', { detail: { message: 'You will be logged out.' } }); window.dispatchEvent(logoutEvent); // Notify the user in their browser /* console.log(logoutResponse.data.redirect_url) */ /* setTimeout(() => { button.text(kwf_translate.disconnect_classroom); }, 60000); */ } else { // Handle logout failure console.error('Logout failed:', logoutResponse.data); } }, error: function (error) { // Handle logout error console.error('Logout error:', error); } }); } else { // Handle sending member IDs failure console.error('Sending member IDs failed:', response.data); } }, error: function (error) { // Handle sending member IDs error console.error('Sending member IDs error:', error); } }); }); and there is global.js that I think I should use to process the logout What I want : **Automatically log out the user with the $user_id and navigate them to the homepage, regardless of their current page. This should occur without the need for a page refresh or button click, and it should apply to the user with the $user_id, not the individual who clicked the button. **
I guess you have two ways of achieving what you need The first would be implementing websockets to allow for asynchronous two-way communication between server and client. I don't know what are the techniques to do so in PHP, but I'm sure they exist Second would be implement good old-fashioned polling on the client side. Based on some interval send a HTTP request using AJAX asking server to tell if the user is still logged in or not. It's not the most elegant solution, but way easier and was utilized heavily when websockets were unavailable (not yet introduced or not yet widly supported by major browsers)

January 7, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.