Skip to content

Instantly share code, notes, and snippets.

@gabrielmerovingi
Created September 22, 2017 10:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielmerovingi/e51ce0da212d814130e4ebd463caef2c to your computer and use it in GitHub Desktop.
Save gabrielmerovingi/e51ce0da212d814130e4ebd463caef2c to your computer and use it in GitHub Desktop.
Daily cron script that will delete log entries that are "older" than a given timestamp.
/**
* Delete Old Entries
* Uses the daily myCRED cron job "mycred_cron_reset_key" to
* delete entries that are older than a given time each day
* @version 1.0
*/
function mycred_pro_delete_old_entries() {
// The maximum age a log entry can have in seconds
// Example 3 months
$max_age = ( 3 * MONTH_IN_SECONDS );
$now = current_time( 'timestamp' );
// Times are stored as unix timestamps so we just deduct the seconds from now
$timestamp = $now - $max_age;
global $wpdb, $mycred;
// Delete entries that are older than our $timestamp
$wpdb->query( $wpdb->prepare( "DELETE FROM {$mycred->log_table} WHERE time < %d;", $timestamp ) );
}
add_action( 'mycred_cron_reset_key', 'mycred_pro_delete_old_entries' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment