Skip to content

Instantly share code, notes, and snippets.

@danielbachhuber
Created September 15, 2020 12:11
Show Gist options
  • Save danielbachhuber/c49a6bccfd2fbeab5512c376dd1c5b36 to your computer and use it in GitHub Desktop.
Save danielbachhuber/c49a6bccfd2fbeab5512c376dd1c5b36 to your computer and use it in GitHub Desktop.
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

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