set_site_transient

Set/update the value of a site transient.

Description

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

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

Parameters (3)

0. $transient (string)
Transient name. Expected to not be SQL-escaped. Must be 40 characters or fewer in length.
1. $value (mixed)
Transient value. 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_site_transient' ) ) { 
  2. require_once ABSPATH . WPINC . '/option.php'; 
  3.  
  4. // Transient name. Expected to not be SQL-escaped. Must be 
  5. // 40 characters or fewer in length. 
  6. $transient = ''; 
  7.  
  8. // Transient value. Expected to not be SQL-escaped. 
  9. $value = null; 
  10.  
  11. // Optional. Time until expiration in seconds. Default 0 (no expiration). 
  12. $expiration = -1; 
  13.  
  14. // NOTICE! Understand what this does before running. 
  15. $result = set_site_transient($transient, $value, $expiration); 
  16.  

Defined (1)

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

/wp-includes/option.php  
  1. function set_site_transient( $transient, $value, $expiration = 0 ) { 
  2.  
  3. /** 
  4. * Filters the value of a specific site transient before it is set. 
  5. * The dynamic portion of the hook name, `$transient`, refers to the transient name. 
  6. * @since 3.0.0 
  7. * @since 4.4.0 The `$transient` parameter was added. 
  8. * @param mixed $value New value of site transient. 
  9. * @param string $transient Transient name. 
  10. */ 
  11. $value = apply_filters( "pre_set_site_transient_{$transient}", $value, $transient ); 
  12.  
  13. $expiration = (int) $expiration; 
  14.  
  15. /** 
  16. * Filters the expiration for a site transient before its value is set. 
  17. * The dynamic portion of the hook name, `$transient`, refers to the transient name. 
  18. * @since 4.4.0 
  19. * @param int $expiration Time until expiration in seconds. Use 0 for no expiration. 
  20. * @param mixed $value New value of site transient. 
  21. * @param string $transient Transient name. 
  22. */ 
  23. $expiration = apply_filters( "expiration_of_site_transient_{$transient}", $expiration, $value, $transient ); 
  24.  
  25. $result = wp_cache_set( $transient, $value, 'site-transient', $expiration ); 
  26. } else { 
  27. $transient_timeout = '_site_transient_timeout_' . $transient; 
  28. $option = '_site_transient_' . $transient; 
  29. if ( false === get_site_option( $option ) ) { 
  30. if ( $expiration ) 
  31. add_site_option( $transient_timeout, time() + $expiration ); 
  32. $result = add_site_option( $option, $value ); 
  33. } else { 
  34. if ( $expiration ) 
  35. update_site_option( $transient_timeout, time() + $expiration ); 
  36. $result = update_site_option( $option, $value ); 
  37. if ( $result ) { 
  38.  
  39. /** 
  40. * Fires after the value for a specific site transient has been set. 
  41. * The dynamic portion of the hook name, `$transient`, refers to the transient name. 
  42. * @since 3.0.0 
  43. * @since 4.4.0 The `$transient` parameter was added 
  44. * @param mixed $value Site transient value. 
  45. * @param int $expiration Time until expiration in seconds. 
  46. * @param string $transient Transient name. 
  47. */ 
  48. do_action( "set_site_transient_{$transient}", $value, $expiration, $transient ); 
  49.  
  50. /** 
  51. * Fires after the value for a site transient has been set. 
  52. * @since 3.0.0 
  53. * @param string $transient The name of the site transient. 
  54. * @param mixed $value Site transient value. 
  55. * @param int $expiration Time until expiration in seconds. 
  56. */ 
  57. do_action( 'setted_site_transient', $transient, $value, $expiration ); 
  58. return $result;