Here is a quick way to add an automatic logout feature to your WordPress powered website. Add this code to your child themes functions.php file. Don’t know what a child theme is? Click here.
The Auto Logout PHP
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php /** * Auto logout ajax. */ add_action( 'wp_ajax_auto_logout', 'auto_logout' ); add_action( 'wp_ajax_nopriv_auto_logout', 'auto_logout' ); function auto_logout(){ wp_verify_nonce($_POST['security', 'secure-my-logout'); echo "Logged Out."; if ($_POST['time']) { wp_clear_auth_cookie(); die(); } die(); } |
Put this javascript in a separate file. Note its file path as you will need to enqueue the script later.
auto-logout.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
"use strict"; jQuery(document).ready(function() { if( jQuery('body').hasClass('logged-in') ) { auto_logout.init(); } }); var auto_logout = { timeoutID : 0, init : function() { document.addEventListener("mousemove", auto_logout.resetTimer, false); document.addEventListener("mousedown", auto_logout.resetTimer, false); document.addEventListener("keypress", auto_logout.resetTimer, false); document.addEventListener("DOMMouseScroll", auto_logout.resetTimer, false); document.addEventListener("mousewheel", auto_logout.resetTimer, false); document.addEventListener("touchmove", auto_logout.resetTimer, false); document.addEventListener("MSPointerMove", auto_logout.resetTimer, false); }, startTimer : function() { auto_logout.timeoutID = window.setTimeoutauto_logout.goInactive, 180000); }, resetTimer : function() { window.clearTimeout(auto_logout.timeoutID); auto_logout.goActive(); }, endTimmer : function() { document.removeEventListener("mousemove", auto_logout.resetTimer, false); document.removeEventListener("mousedown", auto_logout.resetTimer, false); document.removeEventListener("keypress", auto_logout.resetTimer, false); document.removeEventListener("DOMMouseScroll", auto_logout.resetTimer, false); document.removeEventListener("mousewheel", auto_logout.resetTimer, false); document.removeEventListener("touchmove", auto_logout.resetTimer, false); document.removeEventListener("MSPointerMove", auto_logout.resetTimer, false); }, goInactive : function() { var data = { 'action': 'auto_logout', 'time': true, 'ajaxsecurity': ajax_object.logout_nonce, }; jQuery.post(ajax_object.ajax_url, data, function(response) { auto_logout.endTimmer(); alert('You have been logged out due to inactivity.'); window.location.href = "/login/"; }); }, goActive : function() { auto_logout.startTimer(); }, }; |
Don’t forget to enqueue the script and localize it for the ajax to work. Do this in your child theme’s functions.php file. You are using one of those right?
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// Action to enqueue style/scripts. add_action( 'wp_enqueue_scripts', 'prefix_enqueue_scripts' ); function prefix_enqueue_scripts(){ wp_enqueue_script( 'auto-logout', 'path to your auto-logout.js file', array('jquery'), '', true ); // Object properties are accessed as ajax_object.ajax_url, ajax_object.we_value. wp_localize_script( 'auto-logout', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'security' => wp_create_nonce('secure-my-logout') ) } |
Comments are closed.