Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to purge the options table of wp session entries automatically

I'm looking for a way to automatically purge the database of _wp_session records in WordPress.

The SQL query I have is:

DELETE
FROM `wp_options`
WHERE option_name LIKE '_wp_session%'

I would like to know how I can run it once per day with a plugin, PHP script or cron job.

Thanks!

like image 970
user2718437 Avatar asked Mar 15 '23 17:03

user2718437


1 Answers

You should schedule an event that occurs daily. You can do this for example in a custom plugin.

register_activation_hook(__FILE__, 'my_activation');
add_action('my_daily_event', 'do_this_daily');

function my_activation() {
    wp_schedule_event(time(), 'daily', 'my_daily_event');
}

function do_this_daily() {
    $wpdb->query(
      "DELETE FROM $wpdb->options
       WHERE option_name LIKE '_wp_session%'
      "
    )
}
like image 110
TeeDeJee Avatar answered Mar 18 '23 05:03

TeeDeJee