Last active
December 14, 2015 10:19
-
-
Save Pross/5071147 to your computer and use it in GitHub Desktop.
Delete transients older than 7 days.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class PurgeTransients { | |
var $expire = '7 days'; | |
function __construct(){ | |
add_action( 'admin_init', array( &$this, 'init' ) ); | |
} | |
function init(){ | |
$this->purge( $this->expire ); | |
} | |
function purge( $older_than ) { | |
global $wpdb; | |
$older_than_time = strtotime('-' . $older_than); | |
if ( $older_than_time > time() || $older_than_time < 1 ) { | |
return false; | |
} | |
$transients = $wpdb->get_col( | |
$wpdb->prepare( " | |
SELECT REPLACE(option_name, '_transient_timeout_', '') AS transient_name | |
FROM {$wpdb->options} | |
WHERE option_name LIKE '\_transient\_timeout\__%%' | |
AND option_value < %s | |
", $older_than_time ) | |
); | |
if( ! is_array( $transients ) ) | |
return; | |
$option_names = array(); | |
foreach( $transients as $transient ) { | |
$option_names[] = '_transient_' . $transient; | |
$option_names[] = '_transient_timeout_' . $transient; | |
} | |
if ( ! empty( $option_names ) ) { | |
foreach( $option_names as $option ) | |
$result = $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name = '{$option}'" ); | |
} | |
} | |
} | |
new PurgeTransients; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment