Skip to content

Instantly share code, notes, and snippets.

@scofennell
Last active March 23, 2020 15:55
Show Gist options
  • Save scofennell/ff437e45d2de06fd180ee86b8824e34e to your computer and use it in GitHub Desktop.
Save scofennell/ff437e45d2de06fd180ee86b8824e34e to your computer and use it in GitHub Desktop.
WordPress function for deleting all transients related to a certain namespace
<?php
// Purge all the transients associated with our plugin.
function purge() {
global $wpdb;
$prefix = esc_sql( $this -> get_transient_prefix() );
$options = $wpdb -> options;
$t = esc_sql( "_transient_timeout_$prefix%" );
$sql = $wpdb -> prepare (
"
SELECT option_name
FROM $options
WHERE option_name LIKE '%s'
",
$t
);
$transients = $wpdb -> get_col( $sql );
// For each transient...
foreach( $transients as $transient ) {
// Strip away the WordPress prefix in order to arrive at the transient key.
$key = str_replace( '_transient_timeout_', '', $transient );
// Now that we have the key, use WordPress core to the delete the transient.
delete_transient( $key );
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment