set_transient

Set/update the value of a transient.

Description

set_transient( (string) $transient, (mixed) $value, (int) $expiration = 0 ); 

You do not need to serialize values. If the value needs to be serialized, then it will be serialized before it is set.

Parameters (3)

0. $transient (string)
Transient name. Expected to not be SQL-escaped. Must be 172 characters or fewer in length.
1. $value (mixed)
Transient value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
2. $expiration — Optional. (int)
Time until expiration in seconds. Default 0 (no expiration).

Usage

  1. if ( !function_exists( 'set_transient' ) ) { 
  2. require_once ABSPATH . WPINC . '/option.php'; 
  3.  
  4. // Transient name. Expected to not be SQL-escaped. Must be 
  5. // 172 characters or fewer in length. 
  6. $transient = ''; 
  7.  
  8. // Transient value. Must be serializable if non-scalar. 
  9. // Expected to not be SQL-escaped. 
  10. $value = null; 
  11.  
  12. // Optional. Time until expiration in seconds. Default 0 (no expiration). 
  13. $expiration = -1; 
  14.  
  15. // NOTICE! Understand what this does before running. 
  16. $result = set_transient($transient, $value, $expiration); 
  17.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/option.php  
  1. function set_transient( $transient, $value, $expiration = 0 ) { 
  2.  
  3. $expiration = (int) $expiration; 
  4.  
  5. /** 
  6. * Filters a specific transient before its value is set. 
  7. * The dynamic portion of the hook name, `$transient`, refers to the transient name. 
  8. * @since 3.0.0 
  9. * @since 4.2.0 The `$expiration` parameter was added. 
  10. * @since 4.4.0 The `$transient` parameter was added. 
  11. * @param mixed $value New value of transient. 
  12. * @param int $expiration Time until expiration in seconds. 
  13. * @param string $transient Transient name. 
  14. */ 
  15. $value = apply_filters( "pre_set_transient_{$transient}", $value, $expiration, $transient ); 
  16.  
  17. /** 
  18. * Filters the expiration for a transient before its value is set. 
  19. * The dynamic portion of the hook name, `$transient`, refers to the transient name. 
  20. * @since 4.4.0 
  21. * @param int $expiration Time until expiration in seconds. Use 0 for no expiration. 
  22. * @param mixed $value New value of transient. 
  23. * @param string $transient Transient name. 
  24. */ 
  25. $expiration = apply_filters( "expiration_of_transient_{$transient}", $expiration, $value, $transient ); 
  26.  
  27. $result = wp_cache_set( $transient, $value, 'transient', $expiration ); 
  28. } else { 
  29. $transient_timeout = '_transient_timeout_' . $transient; 
  30. $transient_option = '_transient_' . $transient; 
  31. if ( false === get_option( $transient_option ) ) { 
  32. $autoload = 'yes'; 
  33. if ( $expiration ) { 
  34. $autoload = 'no'; 
  35. add_option( $transient_timeout, time() + $expiration, '', 'no' ); 
  36. $result = add_option( $transient_option, $value, '', $autoload ); 
  37. } else { 
  38. // If expiration is requested, but the transient has no timeout option,  
  39. // delete, then re-create transient rather than update. 
  40. $update = true; 
  41. if ( $expiration ) { 
  42. if ( false === get_option( $transient_timeout ) ) { 
  43. delete_option( $transient_option ); 
  44. add_option( $transient_timeout, time() + $expiration, '', 'no' ); 
  45. $result = add_option( $transient_option, $value, '', 'no' ); 
  46. $update = false; 
  47. } else { 
  48. update_option( $transient_timeout, time() + $expiration ); 
  49. if ( $update ) { 
  50. $result = update_option( $transient_option, $value ); 
  51.  
  52. if ( $result ) { 
  53.  
  54. /** 
  55. * Fires after the value for a specific transient has been set. 
  56. * The dynamic portion of the hook name, `$transient`, refers to the transient name. 
  57. * @since 3.0.0 
  58. * @since 3.6.0 The `$value` and `$expiration` parameters were added. 
  59. * @since 4.4.0 The `$transient` parameter was added. 
  60. * @param mixed $value Transient value. 
  61. * @param int $expiration Time until expiration in seconds. 
  62. * @param string $transient The name of the transient. 
  63. */ 
  64. do_action( "set_transient_{$transient}", $value, $expiration, $transient ); 
  65.  
  66. /** 
  67. * Fires after the value for a transient has been set. 
  68. * @since 3.0.0 
  69. * @since 3.6.0 The `$value` and `$expiration` parameters were added. 
  70. * @param string $transient The name of the transient. 
  71. * @param mixed $value Transient value. 
  72. * @param int $expiration Time until expiration in seconds. 
  73. */ 
  74. do_action( 'setted_transient', $transient, $value, $expiration ); 
  75. return $result;