Description

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.

Paramètres

$transient

(string) (Requis) Nom du transient. Doit avoir au plus 172 caractères.

$value

(mixed) (Requis) Valeur du transient. Doit être sérializée si la valeur n'est pas un scalaire.

$expiration

(int) (Optionnel) Temps d'expiration en seconde.

Valeur par défaut : 0

Retourne

(bool) True si la valeur a été réglé, false sinon.

Structure de la fonction set_transient()

Définie dans le fichier wp-includes/option.php à la ligne 842 :

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() ) {
        $result = wp_cache_set( $transient, $value, 'transient', $expiration );
    } else {
        $transient_timeout = '_transient_timeout_' . $transient;
        $transient_option  = '_transient_' . $transient;

        if ( false === get_option( $transient_option ) ) {
            $autoload = 'yes';
            if ( $expiration ) {
                $autoload = 'no';
                add_option( $transient_timeout, time() + $expiration, '', 'no' );
            }
            $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, '', 'no' );
                    $result = add_option( $transient_option, $value, '', 'no' );
                    $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 et Hooks utilisés par set_transient()

expiration_of_transient_{$transient}

Filtre l'expiration du transient avant qu'il ne soit enregistré.

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.

pre_set_transient_{$transient}

Filtre un transient spécifique avant que sa valeur ne soit réglée.

set_transient_{$transient}

Se lance après que la valeur pour un transient spécifique ait été réglé.

setted_transient

Se lance après que la valeur pour un transient ait été réglé.

add_option()

Ajoute une nouvelle option.

delete_option()

Supprime une option par son nom. Empêche de supprimer les options protégées par WordPress.

update_option()

Met à jour la valeur d'une option.

get_option()

Retourne une valeur d'option en fonction de son nom.

Où trouver la fonction set_transient() dans le CMS Wordpress

Exemple

Sauvegarder l'objet $special_query_results pendant 12 heures :
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

Retour