Skip to content

Instantly share code, notes, and snippets.

@kellenmace
Created June 22, 2016 18:13
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kellenmace/55fe09bf913c958dec0d1d9ffd8575f0 to your computer and use it in GitHub Desktop.
Save kellenmace/55fe09bf913c958dec0d1d9ffd8575f0 to your computer and use it in GitHub Desktop.
<?php
/**
* Searches the database for transients stored there that match a specific prefix.
*
* @author Brad Parbs, Kellen Mace
* @param string $prefix prefix to search for.
* @return array nested array response for wpdb->get_results.
*/
function wds_campbell_search_database_for_transients_by_prefix( $prefix ) {
global $wpdb;
// Add our prefix after concating our prefix with the _transient prefix
$prefix = $wpdb->esc_like( '_transient_' . $prefix . '_' );
// Build up our SQL query
$sql = "SELECT `option_name` FROM $wpdb->options WHERE `option_name` LIKE '%s'";
// Execute our query
$transients = $wpdb->get_results( $wpdb->prepare( $sql, $prefix . '%' ), ARRAY_A );
// If if looks good, pass it back
if ( $transients && ! is_wp_error( $transients ) ) {
return $transients;
}
// Otherise return false
return false;
}
/**
* Expects a passed in multidimensional array of transient keys
* array(
* array( 'option_name' => '_transient_blah_blah' ),
* array( 'option_name' => 'transient_another_one' ),
* Can also pass in an array of transient names
*
* @author Brad Parbs, Kellen Mace
* @param array|string $transients Nested array of transienets, keyed by option_name,
* or array of names of transients.j
* @return array Count of total vs deleted.
*/
function wds_campbell_delete_transients_from_keys( $transients ) {
if ( ! isset( $transients ) ) {
return false;
}
// If we get a string key passed in, might as well use it correctly
if ( is_string( $transients ) ) {
$transients = array( array( 'option_name' => $transients ) );
}
// If its not an array, we can't do anything
if ( ! is_array( $transients ) ) {
return false;
}
$results = array();
// Loop through our transients
foreach ( $transients as $transient ) {
if ( is_array( $transient ) ) {
// If we have an array, grab the first element
$transient = current( $transient );
}
// Remove that sucker
$results[ $transient ] = delete_transient( str_replace( '_transient_', '', $transient ) );
}
// Return an array of total number, and number deleted
return array(
'total' => count( $results ),
'deleted' => array_sum( $results ),
);
}
/**
* Delete brands select menu transients
*
* @author Kellen Mace
*/
function wds_delete_brands_region_transients() {
wds_campbell_delete_transients_from_keys( wds_campbell_search_database_for_transients_by_prefix( 'wds_campbell_brand_regions' ) );
}
add_action( 'create_region', 'wds_delete_brands_region_transients' );
add_action( 'edit_region', 'wds_delete_brands_region_transients' );
add_action( 'delete_region', 'wds_delete_brands_region_transients' );
@benjaminpick
Copy link

Line 15 - https://gist.github.com/kellenmace/55fe09bf913c958dec0d1d9fafd8575f0#file-delete-transients-by-prefix-php-L15
I needed to delete the . '_' at the end to make it work (maybe Wordpress changed the default transient name)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment