Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Automatically clear WP Native PHP Sessions older than 24 hours
<?php
add_action(
'init',
function() {
if ( ! wp_next_scheduled( 'pantheonx_clear_sessions' ) ) {
wp_schedule_event( time(), 'twicedaily', 'pantheonx_clear_sessions' );
}
}
);
add_action(
'pantheonx_clear_sessions',
function() {
global $wpdb, $table_prefix;
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$table_prefix}pantheon_sessions WHERE `datetime` < %s",
gmdate( 'Y-m-d H:i:s', strtotime( '24 hours ago' ) )
)
);
}
);
@msulaimanmisri
Copy link

msulaimanmisri commented Jan 25, 2021

Hello. Do I need to install any plugin in order to make this can run on my wordpress site?

and how to set to delete after 7 days?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment