Last active
March 23, 2020 15:55
-
-
Save scofennell/ff437e45d2de06fd180ee86b8824e34e to your computer and use it in GitHub Desktop.
WordPress function for deleting all transients related to a certain namespace
This file contains hidden or 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 | |
// 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