set_transient( string $transient, mixed $value, int $expiration = 0 )
Enregistrer, mettre à jour le transient.
Paramètres
$transient(string)requisNom du transient. Doit avoir au plus 172 caractères.
$value(mixed)requisValeur du transient. Doit être sérializée si la valeur n'est pas un scalaire.
$expiration(int)optionnelTemps d'expiration en seconde.
Valeur par défaut : 0
Description / Informations supplémentaires
Si la valeur a besoin d'être sérializée, elle le sera avant d'être enregistrée.
Si $expiration est omis le transient n'expire jamais.
Retourne
(bool) True si la valeur a été réglé, false sinon.
Déclaration et structure de la fonction set_transient()
set_transient() est déclarée dans le fichier wp-includes/option.php à la ligne 1477 :
function set_transient( $transient, $value, $expiration = 0 ) {
$expiration = (int) $expiration;
/**
* Filters a specific transient before its value is set.
*
* The dynamic portion of the hook name, `$transient`, refers to the transient name.
*
* @since 3.0.0
* @since 4.2.0 The `$expiration` parameter was added.
* @since 4.4.0 The `$transient` parameter was added.
*
* @param mixed $value New value of transient.
* @param int $expiration Time until expiration in seconds.
* @param string $transient Transient name.
*/
$value = apply_filters( "pre_set_transient_{$transient}", $value, $expiration, $transient );
/**
* Filters the expiration for a transient before its value is set.
*
* The dynamic portion of the hook name, `$transient`, refers to the transient name.
*
* @since 4.4.0
*
* @param int $expiration Time until expiration in seconds. Use 0 for no expiration.
* @param mixed $value New value of transient.
* @param string $transient Transient name.
*/
$expiration = apply_filters( "expiration_of_transient_{$transient}", $expiration, $value, $transient );
if ( wp_using_ext_object_cache() || wp_installing() ) {
$result = wp_cache_set( $transient, $value, 'transient', $expiration );
} else {
$transient_timeout = '_transient_timeout_' . $transient;
$transient_option = '_transient_' . $transient;
wp_prime_option_caches( array( $transient_option, $transient_timeout ) );
if ( false === get_option( $transient_option ) ) {
$autoload = true;
if ( $expiration ) {
$autoload = false;
add_option( $transient_timeout, time() + $expiration, '', false );
}
$result = add_option( $transient_option, $value, '', $autoload );
} else {
/*
* If expiration is requested, but the transient has no timeout option,
* delete, then re-create transient rather than update.
*/
$update = true;
if ( $expiration ) {
if ( false === get_option( $transient_timeout ) ) {
delete_option( $transient_option );
add_option( $transient_timeout, time() + $expiration, '', false );
$result = add_option( $transient_option, $value, '', false );
$update = false;
} else {
update_option( $transient_timeout, time() + $expiration );
}
}
if ( $update ) {
$result = update_option( $transient_option, $value );
}
}
}
if ( $result ) {
/**
* Fires after the value for a specific transient has been set.
*
* The dynamic portion of the hook name, `$transient`, refers to the transient name.
*
* @since 3.0.0
* @since 3.6.0 The `$value` and `$expiration` parameters were added.
* @since 4.4.0 The `$transient` parameter was added.
*
* @param mixed $value Transient value.
* @param int $expiration Time until expiration in seconds.
* @param string $transient The name of the transient.
*/
do_action( "set_transient_{$transient}", $value, $expiration, $transient );
/**
* Fires after the value for a transient has been set.
*
* @since 3.0.0
* @since 3.6.0 The `$value` and `$expiration` parameters were added.
*
* @param string $transient The name of the transient.
* @param mixed $value Transient value.
* @param int $expiration Time until expiration in seconds.
*/
do_action( 'setted_transient', $transient, $value, $expiration );
}
return $result;
}
Fonctions utilisées par set_transient()
wp_cache_set()Sauvegarde les données dans le cache.
wp_using_ext_object_cache()Fait basculer $_wp_using_ext_object_cache sur on ou off sans toucher à la variable.
apply_filters()Appel les fonctions qui ont été attaché à un filtre (hook).
do_action()Exécute des fonctions attachées à un hook spécifique.
add_option()Ajoute une nouvelle option.
delete_option()Supprime une option par son nom. Préviens si cette option appartient aux options natives de WordPress.
update_option()Met à jour la valeur d'une option.
get_option()Retourne une valeur d'option en fonction de son nom.
Hooks utilisés par set_transient()
expiration_of_transient_transientFiltre l'expiration du transient avant qu'il ne soit enregistré.
pre_set_transient_transientFiltre un transient spécifique avant que sa valeur ne soit réglée.
set_transient_transientSe lance après que la valeur pour un transient spécifique ait été réglé.
setted_transientSe lance après que la valeur pour un transient ait été réglé.
Où est utilisée la fonction set_transient()
dans le CMS WordPress
Exemple
set_transient( 'special_query_results', $special_query_results, 12 * HOUR_IN_SECONDS );
Sources
Codex WordPress : set_transient()
Autres fonctions dans le même fichier : wp-includes/option.php