Ryan Kanner (@CodeProKid)
http://i.imgur.com/EAvvfw2.gifv
if ( false === ( $data = get_transient( 'my_transient' ) ) ) {
$data = 'some stuff';
set_transient( 'my_transient', $data, HOUR_IN_SECONDS );
}
echo $data;
Transient expiration times are a maximum time. There is no minimum age. Transients might disappear one second after you set them, or 24 hours, but they will never be around after the expiration time.
$transient(string) - Unique name to use when saving the transient. Becomes the "Key" in the key => value.
$value(mixed) - Data to be stored in the the transient. Can be an array, string, or an integer.
$expiration(int) - Optional integer passed to set max age. Adds _transient_timeout_$transient setting to options table, or uses built in object cache expiration.
@return(bool) - Returns true if successful, false if unsuccessful.
set_transient( $transient, $value, $expiration );
set_transient Hooks & Filters
pre_set_transient_{$transient} - exposes the value to be stored to the transient to a filter. Expiration and transient name are passed as context.
expiration_of_transient_{$transient} - exposes the expiration time to a filter. Transient name and value passed as context
set_transient_{$transient} - action that fires after a specific transient has been saved. Transient name, Value, and expiration passed as context.
setted_transient - action that fires after any transient is saved. Transient name, Value, and expiration passed as context.
$transient(string) - Name of the transient to retrieve. Will use get_option() to retrieve a transient saved in the database. Will use wp_cache_get() if saved in the object cache.
This function does a check to see if the transient data has expired. If it has, it will delete the data from the storage engine.
@return (bool|mixed) - If the transient is expired, or it doesn't exist, it will return false. Otherwise it will return the data stored in the transient.
get_transient( $transient );
get_transient Hooks & Filters
pre_transient_$transient - By default this value is set to false. This can be set to true via this filter to short circuit the process of returning a transients value. Transient name is passed as context.
transient_$transient - exposes value of the transient being returned to a filter. Transient name and value passed as context.
$transient(string) - Name of the transient to delete. Will use delete_option() to delete a transient saved in the database. Will use wp_cache_delete() if saved in the object cache.
This function will also delete the timeout (expiration) value from the database.
@return (bool) - Returns true if deletion is successful, false if it isn't.
delete_transient( $transient );
delete_transient Hooks & Filters
delete_transient_$transient - action that fires before a transient is actually deleted. Transient name is passed as context.
deleted_transient - action that fires after any transient is deleted. Transient name is passed as context.
if ( false === ( $photos = get_transient( 'instagram_photos' ) ) ) {
$url = add_query_arg(
[
'access_token' => 'xxx-xxx-xxxx',
'count' => 30,
],
'https://api.instagram.com/v1/users/1234/media/recent/'
);
$response = wp_remote_get( esc_url( $url ), [ 'timeout' => 600 ] );
$photos = $response['body'];
set_transient( 'instagram_photos', $photos, 3 * HOUR_IN_SECONDS );
}
print_r( $photos );
if ( false === ( $query = get_transient( 'query_transient' ) ) {
$args = [
'post_type' => 'post',
'posts_per_page' => 100,
'fields' => 'ids',
'meta_query' => array(
'AND',
array(
'key' => 'key_1',
'value' => 'value_1',
),
array(
'key' => 'key_2',
'value' => 'value_2',
),
),
];
$post_ids = new WP_Query( $args );
set_transient( 'query_transient', $post_ids, 0 );
}
echo '<ul>';
foreach ( $post_ids as $post_id ) {
echo '<li>' . get_the_title( $post_id ) . '</li>';
}
echo '</ul>';
function transient_flusher( $meta_id, $object_id, $meta_key ) {
if ( 'key_1' === $meta_key || 'key_2' === $meta_key ) {
delete_transient( 'query_transient' );
}
}
add_action( 'updated_post_meta', 'transient_flusher', 10, 3 );
http://dlopez1986.blogspot.com/2012/09/trying-to-make-animated-gifs.html
// Register the transient
function register_sample_transient() {
$transient_args = array(
'cache_type' => 'transient',
'callback' => 'transient_callback',
'expiration' => DAY_IN_SECONDS,
'soft_expiration' => true,
'async_updates' => true,
'update_hooks' => array(
'updated_post_meta' => 'transient_meta_update_cb',
),
);
dfm_register_transient( 'sample_transient', $transient_args );
}
add_action( 'after_setup_theme', 'register_sample_transient' );
// Callback to populate the data to store in the transient
function transient_callback( $modifier ) {
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 5,
'tax_query' => array(
array(
'taxonomy' => 'category',
'terms' => $modifier,
),
),
);
$posts = new WP_Query( $args );
return $posts;
}
// Callback to decide if we should actually regenerate the data on this hook
function transient_meta_update_cb( $args ) {
// Matches $meta_key value (3rd arg passed to hook)
if ( 'my_meta_key' === $args[2] ) {
// Returns post ID
return $args[1]
} else {
// If this callback returns false, we will not regenerate the transient data.
return false;
}
http://gph.is/1QW9HqB