Logout all users at particular time
How can I logout all users in wordpress at specific time? I tried to create a hook and used WP Control plugin but it did not worked for me
How can I logout all users in wordpress at specific time? I tried to create a hook and used WP Control plugin but it did not worked for me
@C0c0b33f nearly has the correct answer, but to log out all users, it is necessary to use the WP_Session_Tokens class. If you change the myplugin_cron_function
in the previous answer to the following it should work.
// hook function myplugin_cron_hook() to the action myplugin_cron_hook.
add_action( 'myplugin_cron_hook', 'myplugin_cron_function' );
/**
* Create hook and schedule cron job on plugin activation.
* Schedule recurring cron event.
* hourly
* twicedaily
* daily
*/
function myplugin_activate() {
$timestamp = strtotime( '24:00:00' ); // 12:00 AM.
// check to make sure it's not already scheduled.
if ( ! wp_next_scheduled( 'myplugin_cron_hook' ) ) {
wp_schedule_event( $timestamp, 'daily', 'myplugin_cron_hook' );
// use wp_schedule_single_event function for non-recurring.
}
}
register_activation_hook( __FILE__, 'myplugin_activate' );
/**
* Unset cron event on plugin deactivation.
*/
function myplugin_deactivate() {
wp_clear_scheduled_hook( 'myplugin_cron_hook' ); // unschedule event.
}
register_deactivation_hook( __FILE__, 'myplugin_deactivate' );
/**
* Function called by the cron event.
*/
function myplugin_cron_function() {
// Get an instance of WP_User_Meta_Session_Tokens
$sessions_manager = WP_Session_Tokens::get_instance();
// Remove all the session data for all users.
$sessions_manager->drop_sessions();
}
You could use WP Cron to fire wp_logout() and make it more reliable by following instructions here https://developer.wordpress.org/plugins/cron/hooking-wp-cron-into-the-system-task-scheduler/
Below is code for a plugin that would log everyone out, every day at midnight. Note if you wanted to schedule a one-time-only, single event, use wp_schedule_single_event instead of wp_schedule_event https://codex.wordpress.org/Function_Reference/wp_schedule_single_event
// hook function myplugin_cron_hook() to the action myplugin_cron_hook.
add_action( 'myplugin_cron_hook', 'myplugin_cron_function' );
/**
* Create hook and schedule cron job on plugin activation.
* Schedule recurring cron event.
* hourly
* twicedaily
* daily
*/
function myplugin_activate() {
$timestamp = strtotime( '24:00:00' ); // 12:00 AM.
// check to make sure it's not already scheduled.
if ( ! wp_next_scheduled( 'myplugin_cron_hook' ) ) {
wp_schedule_event( $timestamp, 'daily', 'myplugin_cron_hook' );
// use wp_schedule_single_event function for non-recurring.
}
}
register_activation_hook( __FILE__, 'myplugin_activate' );
/**
* Unset cron event on plugin deactivation.
*/
function myplugin_deactivate() {
wp_clear_scheduled_hook( 'myplugin_cron_hook' ); // unschedule event.
}
register_deactivation_hook( __FILE__, 'myplugin_deactivate' );
/**
* Function called by the cron event.
*/
function myplugin_cron_function() {
// error_log( print_r( 'yes we have wp-cron', true ) );
wp_logout();
}
/**
* View all currently scheduled tasks utility.
*/
function myplugin_print_tasks() {
echo '<pre>';
print_r( _get_cron_array() );
echo '</pre>';
}
Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.