9 * Retrieve option value based on name of option.
11 * If the option does not exist or does not have a value, then the return value
12 * will be false. This is useful to check whether you need to install an option
13 * and is commonly used during installation of plugin options and to test
14 * whether upgrading is required.
16 * If the option was serialized then it will be unserialized when it is returned.
21 * @uses apply_filters() Calls 'pre_option_$option' before checking the option.
22 * Any value other than false will "short-circuit" the retrieval of the option
23 * and return the returned value. You should not try to override special options,
24 * but you will not be prevented from doing so.
25 * @uses apply_filters() Calls 'option_$option', after checking the option, with
28 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
29 * @param mixed $default Optional. Default value to return if the option does not exist.
30 * @return mixed Value set for the option.
32 function get_option( $option, $default = false ) {
35 // Allow plugins to short-circuit options.
36 $pre = apply_filters( 'pre_option_' . $option, false );
40 $option = trim($option);
44 if ( defined( 'WP_SETUP_CONFIG' ) )
47 if ( ! defined( 'WP_INSTALLING' ) ) {
48 // prevent non-existent options from triggering multiple queries
49 $notoptions = wp_cache_get( 'notoptions', 'options' );
50 if ( isset( $notoptions[$option] ) )
51 return apply_filters( 'default_option_' . $option, $default );
53 $alloptions = wp_load_alloptions();
55 if ( isset( $alloptions[$option] ) ) {
56 $value = $alloptions[$option];
58 $value = wp_cache_get( $option, 'options' );
60 if ( false === $value ) {
61 $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
63 // Has to be get_row instead of get_var because of funkiness with 0, false, null values
64 if ( is_object( $row ) ) {
65 $value = $row->option_value;
66 wp_cache_add( $option, $value, 'options' );
67 } else { // option does not exist, so we must cache its non-existence
68 $notoptions[$option] = true;
69 wp_cache_set( 'notoptions', $notoptions, 'options' );
70 return apply_filters( 'default_option_' . $option, $default );
75 $suppress = $wpdb->suppress_errors();
76 $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
77 $wpdb->suppress_errors( $suppress );
78 if ( is_object( $row ) )
79 $value = $row->option_value;
81 return apply_filters( 'default_option_' . $option, $default );
84 // If home is not set use siteurl.
85 if ( 'home' == $option && '' == $value )
86 return get_option( 'siteurl' );
88 if ( in_array( $option, array('siteurl', 'home', 'category_base', 'tag_base') ) )
89 $value = untrailingslashit( $value );
91 return apply_filters( 'option_' . $option, maybe_unserialize( $value ) );
95 * Protect WordPress special option from being modified.
97 * Will die if $option is in protected list. Protected options are 'alloptions'
98 * and 'notoptions' options.
104 * @param string $option Option name.
106 function wp_protect_special_option( $option ) {
107 $protected = array( 'alloptions', 'notoptions' );
108 if ( in_array( $option, $protected ) )
109 wp_die( sprintf( __( '%s is a protected WP option and may not be modified' ), esc_html( $option ) ) );
113 * Print option value after sanitizing for forms.
115 * @uses attr Sanitizes value.
120 * @param string $option Option name.
122 function form_option( $option ) {
123 echo esc_attr( get_option( $option ) );
127 * Loads and caches all autoloaded options, if available or all options.
133 * @return array List of all options.
135 function wp_load_alloptions() {
138 if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
139 $alloptions = wp_cache_get( 'alloptions', 'options' );
143 if ( !$alloptions ) {
144 $suppress = $wpdb->suppress_errors();
145 if ( !$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) )
146 $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
147 $wpdb->suppress_errors($suppress);
148 $alloptions = array();
149 foreach ( (array) $alloptions_db as $o ) {
150 $alloptions[$o->option_name] = $o->option_value;
152 if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
153 wp_cache_add( 'alloptions', $alloptions, 'options' );
160 * Loads and caches certain often requested site options if is_multisite() and a persistent cache is not being used.
166 * @param int $site_id Optional site ID for which to query the options. Defaults to the current site.
168 function wp_load_core_site_options( $site_id = null ) {
169 global $wpdb, $_wp_using_ext_object_cache;
171 if ( !is_multisite() || $_wp_using_ext_object_cache || defined( 'WP_INSTALLING' ) )
174 if ( empty($site_id) )
175 $site_id = $wpdb->siteid;
177 $core_options = array('site_name', 'siteurl', 'active_sitewide_plugins', '_site_transient_timeout_theme_roots', '_site_transient_theme_roots', 'site_admins', 'can_compress_scripts', 'global_terms_enabled' );
179 $core_options_in = "'" . implode("', '", $core_options) . "'";
180 $options = $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value FROM $wpdb->sitemeta WHERE meta_key IN ($core_options_in) AND site_id = %d", $site_id) );
182 foreach ( $options as $option ) {
183 $key = $option->meta_key;
184 $cache_key = "{$site_id}:$key";
185 $option->meta_value = maybe_unserialize( $option->meta_value );
187 wp_cache_set( $cache_key, $option->meta_value, 'site-options' );
192 * Update the value of an option that was already added.
194 * You do not need to serialize values. If the value needs to be serialized, then
195 * it will be serialized before it is inserted into the database. Remember,
196 * resources can not be serialized or added as an option.
198 * If the option does not exist, then the option will be added with the option
199 * value, but you will not be able to set whether it is autoloaded. If you want
200 * to set whether an option is autoloaded, then you need to use the add_option().
206 * @uses apply_filters() Calls 'pre_update_option_$option' hook to allow overwriting the
207 * option value to be stored.
208 * @uses do_action() Calls 'update_option' hook before updating the option.
209 * @uses do_action() Calls 'update_option_$option' and 'updated_option' hooks on success.
211 * @param string $option Option name. Expected to not be SQL-escaped.
212 * @param mixed $newvalue Option value. Expected to not be SQL-escaped.
213 * @return bool False if value was not updated and true if value was updated.
215 function update_option( $option, $newvalue ) {
218 $option = trim($option);
219 if ( empty($option) )
222 wp_protect_special_option( $option );
224 if ( is_object($newvalue) )
225 $newvalue = clone $newvalue;
227 $newvalue = sanitize_option( $option, $newvalue );
228 $oldvalue = get_option( $option );
229 $newvalue = apply_filters( 'pre_update_option_' . $option, $newvalue, $oldvalue );
231 // If the new and old values are the same, no need to update.
232 if ( $newvalue === $oldvalue )
235 if ( false === $oldvalue )
236 return add_option( $option, $newvalue );
238 $notoptions = wp_cache_get( 'notoptions', 'options' );
239 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
240 unset( $notoptions[$option] );
241 wp_cache_set( 'notoptions', $notoptions, 'options' );
244 $_newvalue = $newvalue;
245 $newvalue = maybe_serialize( $newvalue );
247 do_action( 'update_option', $option, $oldvalue, $_newvalue );
248 if ( ! defined( 'WP_INSTALLING' ) ) {
249 $alloptions = wp_load_alloptions();
250 if ( isset( $alloptions[$option] ) ) {
251 $alloptions[$option] = $_newvalue;
252 wp_cache_set( 'alloptions', $alloptions, 'options' );
254 wp_cache_set( $option, $_newvalue, 'options' );
258 $result = $wpdb->update( $wpdb->options, array( 'option_value' => $newvalue ), array( 'option_name' => $option ) );
261 do_action( "update_option_{$option}", $oldvalue, $_newvalue );
262 do_action( 'updated_option', $option, $oldvalue, $_newvalue );
271 * You do not need to serialize values. If the value needs to be serialized, then
272 * it will be serialized before it is inserted into the database. Remember,
273 * resources can not be serialized or added as an option.
275 * You can create options without values and then update the values later.
276 * Existing options will not be updated and checks are performed to ensure that you
277 * aren't adding a protected WordPress option. Care should be taken to not name
278 * options the same as the ones which are protected.
284 * @uses do_action() Calls 'add_option' hook before adding the option.
285 * @uses do_action() Calls 'add_option_$option' and 'added_option' hooks on success.
287 * @param string $option Name of option to add. Expected to not be SQL-escaped.
288 * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
289 * @param mixed $deprecated Optional. Description. Not used anymore.
290 * @param bool $autoload Optional. Default is enabled. Whether to load the option when WordPress starts up.
291 * @return bool False if option was not added and true if option was added.
293 function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) {
296 if ( !empty( $deprecated ) )
297 _deprecated_argument( __FUNCTION__, '2.3' );
299 $option = trim($option);
300 if ( empty($option) )
303 wp_protect_special_option( $option );
305 if ( is_object($value) )
306 $value = clone $value;
308 $value = sanitize_option( $option, $value );
310 // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
311 $notoptions = wp_cache_get( 'notoptions', 'options' );
312 if ( !is_array( $notoptions ) || !isset( $notoptions[$option] ) )
313 if ( false !== get_option( $option ) )
317 $value = maybe_serialize( $value );
318 $autoload = ( 'no' === $autoload ) ? 'no' : 'yes';
319 do_action( 'add_option', $option, $_value );
320 if ( ! defined( 'WP_INSTALLING' ) ) {
321 if ( 'yes' == $autoload ) {
322 $alloptions = wp_load_alloptions();
323 $alloptions[$option] = $value;
324 wp_cache_set( 'alloptions', $alloptions, 'options' );
326 wp_cache_set( $option, $value, 'options' );
330 // This option exists now
331 $notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh
332 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
333 unset( $notoptions[$option] );
334 wp_cache_set( 'notoptions', $notoptions, 'options' );
337 $result = $wpdb->query( $wpdb->prepare( "INSERT INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, %s) ON DUPLICATE KEY UPDATE `option_name` = VALUES(`option_name`), `option_value` = VALUES(`option_value`), `autoload` = VALUES(`autoload`)", $option, $value, $autoload ) );
340 do_action( "add_option_{$option}", $option, $_value );
341 do_action( 'added_option', $option, $_value );
348 * Removes option by name. Prevents removal of protected WordPress options.
354 * @uses do_action() Calls 'delete_option' hook before option is deleted.
355 * @uses do_action() Calls 'deleted_option' and 'delete_option_$option' hooks on success.
357 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
358 * @return bool True, if option is successfully deleted. False on failure.
360 function delete_option( $option ) {
363 wp_protect_special_option( $option );
365 // Get the ID, if no ID then return
366 $row = $wpdb->get_row( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) );
367 if ( is_null( $row ) )
369 do_action( 'delete_option', $option );
370 $result = $wpdb->delete( $wpdb->options, array( 'option_name' => $option ) );
371 if ( ! defined( 'WP_INSTALLING' ) ) {
372 if ( 'yes' == $row->autoload ) {
373 $alloptions = wp_load_alloptions();
374 if ( is_array( $alloptions ) && isset( $alloptions[$option] ) ) {
375 unset( $alloptions[$option] );
376 wp_cache_set( 'alloptions', $alloptions, 'options' );
379 wp_cache_delete( $option, 'options' );
383 do_action( "delete_option_$option", $option );
384 do_action( 'deleted_option', $option );
391 * Delete a transient.
395 * @subpackage Transient
397 * @uses do_action() Calls 'delete_transient_$transient' hook before transient is deleted.
398 * @uses do_action() Calls 'deleted_transient' hook on success.
400 * @param string $transient Transient name. Expected to not be SQL-escaped.
401 * @return bool true if successful, false otherwise
403 function delete_transient( $transient ) {
404 global $_wp_using_ext_object_cache;
406 do_action( 'delete_transient_' . $transient, $transient );
408 if ( $_wp_using_ext_object_cache ) {
409 $result = wp_cache_delete( $transient, 'transient' );
411 $option_timeout = '_transient_timeout_' . $transient;
412 $option = '_transient_' . $transient;
413 $result = delete_option( $option );
415 delete_option( $option_timeout );
419 do_action( 'deleted_transient', $transient );
424 * Get the value of a transient.
426 * If the transient does not exist or does not have a value, then the return value
429 * @uses apply_filters() Calls 'pre_transient_$transient' hook before checking the transient.
430 * Any value other than false will "short-circuit" the retrieval of the transient
431 * and return the returned value.
432 * @uses apply_filters() Calls 'transient_$option' hook, after checking the transient, with
433 * the transient value.
437 * @subpackage Transient
439 * @param string $transient Transient name. Expected to not be SQL-escaped
440 * @return mixed Value of transient
442 function get_transient( $transient ) {
443 global $_wp_using_ext_object_cache;
445 $pre = apply_filters( 'pre_transient_' . $transient, false );
446 if ( false !== $pre )
449 if ( $_wp_using_ext_object_cache ) {
450 $value = wp_cache_get( $transient, 'transient' );
452 $transient_option = '_transient_' . $transient;
453 if ( ! defined( 'WP_INSTALLING' ) ) {
454 // If option is not in alloptions, it is not autoloaded and thus has a timeout
455 $alloptions = wp_load_alloptions();
456 if ( !isset( $alloptions[$transient_option] ) ) {
457 $transient_timeout = '_transient_timeout_' . $transient;
458 if ( get_option( $transient_timeout ) < time() ) {
459 delete_option( $transient_option );
460 delete_option( $transient_timeout );
466 $value = get_option( $transient_option );
469 return apply_filters( 'transient_' . $transient, $value );
473 * Set/update the value of a transient.
475 * You do not need to serialize values. If the value needs to be serialized, then
476 * it will be serialized before it is set.
480 * @subpackage Transient
482 * @uses apply_filters() Calls 'pre_set_transient_$transient' hook to allow overwriting the
483 * transient value to be stored.
484 * @uses do_action() Calls 'set_transient_$transient' and 'setted_transient' hooks on success.
486 * @param string $transient Transient name. Expected to not be SQL-escaped.
487 * @param mixed $value Transient value. Expected to not be SQL-escaped.
488 * @param int $expiration Time until expiration in seconds, default 0
489 * @return bool False if value was not set and true if value was set.
491 function set_transient( $transient, $value, $expiration = 0 ) {
492 global $_wp_using_ext_object_cache;
494 $value = apply_filters( 'pre_set_transient_' . $transient, $value );
496 if ( $_wp_using_ext_object_cache ) {
497 $result = wp_cache_set( $transient, $value, 'transient', $expiration );
499 $transient_timeout = '_transient_timeout_' . $transient;
500 $transient = '_transient_' . $transient;
501 if ( false === get_option( $transient ) ) {
505 add_option( $transient_timeout, time() + $expiration, '', 'no' );
507 $result = add_option( $transient, $value, '', $autoload );
510 update_option( $transient_timeout, time() + $expiration );
511 $result = update_option( $transient, $value );
515 do_action( 'set_transient_' . $transient );
516 do_action( 'setted_transient', $transient );
522 * Saves and restores user interface settings stored in a cookie.
524 * Checks if the current user-settings cookie is updated and stores it. When no
525 * cookie exists (different browser used), adds the last saved cookie restoring
532 function wp_user_settings() {
537 if ( defined('DOING_AJAX') )
540 if ( ! $user = wp_get_current_user() )
543 $settings = get_user_option( 'user-settings', $user->ID );
545 if ( isset( $_COOKIE['wp-settings-' . $user->ID] ) ) {
546 $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user->ID] );
548 if ( ! empty( $cookie ) && strpos( $cookie, '=' ) ) {
549 if ( $cookie == $settings )
552 $last_time = (int) get_user_option( 'user-settings-time', $user->ID );
553 $saved = isset( $_COOKIE['wp-settings-time-' . $user->ID]) ? preg_replace( '/[^0-9]/', '', $_COOKIE['wp-settings-time-' . $user->ID] ) : 0;
555 if ( $saved > $last_time ) {
556 update_user_option( $user->ID, 'user-settings', $cookie, false );
557 update_user_option( $user->ID, 'user-settings-time', time() - 5, false );
563 setcookie( 'wp-settings-' . $user->ID, $settings, time() + 31536000, SITECOOKIEPATH );
564 setcookie( 'wp-settings-time-' . $user->ID, time(), time() + 31536000, SITECOOKIEPATH );
565 $_COOKIE['wp-settings-' . $user->ID] = $settings;
569 * Retrieve user interface setting value based on setting name.
575 * @param string $name The name of the setting.
576 * @param string $default Optional default value to return when $name is not set.
577 * @return mixed the last saved user setting or the default value/false if it doesn't exist.
579 function get_user_setting( $name, $default = false ) {
581 $all = get_all_user_settings();
583 return isset($all[$name]) ? $all[$name] : $default;
587 * Add or update user interface setting.
589 * Both $name and $value can contain only ASCII letters, numbers and underscores.
590 * This function has to be used before any output has started as it calls setcookie().
596 * @param string $name The name of the setting.
597 * @param string $value The value for the setting.
598 * @return bool true if set successfully/false if not.
600 function set_user_setting( $name, $value ) {
602 if ( headers_sent() )
605 $all = get_all_user_settings();
606 $name = preg_replace( '/[^A-Za-z0-9_]+/', '', $name );
611 $all[$name] = $value;
613 return wp_set_all_user_settings($all);
617 * Delete user interface settings.
619 * Deleting settings would reset them to the defaults.
620 * This function has to be used before any output has started as it calls setcookie().
626 * @param mixed $names The name or array of names of the setting to be deleted.
627 * @return bool true if deleted successfully/false if not.
629 function delete_user_setting( $names ) {
631 if ( headers_sent() )
634 $all = get_all_user_settings();
635 $names = (array) $names;
637 foreach ( $names as $name ) {
638 if ( isset($all[$name]) ) {
644 if ( isset($deleted) )
645 return wp_set_all_user_settings($all);
651 * Retrieve all user interface settings.
657 * @return array the last saved user settings or empty array.
659 function get_all_user_settings() {
660 global $_updated_user_settings;
662 if ( ! $user = wp_get_current_user() )
665 if ( isset($_updated_user_settings) && is_array($_updated_user_settings) )
666 return $_updated_user_settings;
669 if ( isset($_COOKIE['wp-settings-' . $user->ID]) ) {
670 $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user->ID] );
672 if ( $cookie && strpos($cookie, '=') ) // the '=' cannot be 1st char
673 parse_str($cookie, $all);
676 $option = get_user_option('user-settings', $user->ID);
677 if ( $option && is_string($option) )
678 parse_str( $option, $all );
685 * Private. Set all user interface settings.
691 * @param unknown $all
694 function wp_set_all_user_settings($all) {
695 global $_updated_user_settings;
697 if ( ! $user = wp_get_current_user() )
700 $_updated_user_settings = $all;
702 foreach ( $all as $k => $v ) {
703 $v = preg_replace( '/[^A-Za-z0-9_]+/', '', $v );
704 $settings .= $k . '=' . $v . '&';
707 $settings = rtrim($settings, '&');
709 update_user_option( $user->ID, 'user-settings', $settings, false );
710 update_user_option( $user->ID, 'user-settings-time', time(), false );
716 * Delete the user settings of the current user.
722 function delete_all_user_settings() {
723 if ( ! $user = wp_get_current_user() )
726 update_user_option( $user->ID, 'user-settings', '', false );
727 setcookie('wp-settings-' . $user->ID, ' ', time() - 31536000, SITECOOKIEPATH);
731 * Retrieve site option value based on name of option.
738 * @uses apply_filters() Calls 'pre_site_option_$option' before checking the option.
739 * Any value other than false will "short-circuit" the retrieval of the option
740 * and return the returned value.
741 * @uses apply_filters() Calls 'site_option_$option', after checking the option, with
744 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
745 * @param mixed $default Optional value to return if option doesn't exist. Default false.
746 * @param bool $use_cache Whether to use cache. Multisite only. Default true.
747 * @return mixed Value set for the option.
749 function get_site_option( $option, $default = false, $use_cache = true ) {
752 // Allow plugins to short-circuit site options.
753 $pre = apply_filters( 'pre_site_option_' . $option, false );
754 if ( false !== $pre )
757 if ( ! is_multisite() ) {
758 $default = apply_filters( 'default_site_option_' . $option, $default );
759 $value = get_option($option, $default);
761 $cache_key = "{$wpdb->siteid}:$option";
763 $value = wp_cache_get($cache_key, 'site-options');
765 if ( !isset($value) || (false === $value) ) {
766 $row = $wpdb->get_row( $wpdb->prepare("SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );
768 // Has to be get_row instead of get_var because of funkiness with 0, false, null values
769 if ( is_object( $row ) ) {
770 $value = $row->meta_value;
771 $value = maybe_unserialize( $value );
772 wp_cache_set( $cache_key, $value, 'site-options' );
774 $value = apply_filters( 'default_site_option_' . $option, $default );
779 return apply_filters( 'site_option_' . $option, $value );
783 * Add a new site option.
785 * Existing options will not be updated. Note that prior to 3.3 this wasn't the case.
792 * @uses apply_filters() Calls 'pre_add_site_option_$option' hook to allow overwriting the
793 * option value to be stored.
794 * @uses do_action() Calls 'add_site_option_$option' and 'add_site_option' hooks on success.
796 * @param string $option Name of option to add. Expected to not be SQL-escaped.
797 * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
798 * @return bool False if option was not added and true if option was added.
800 function add_site_option( $option, $value ) {
803 $value = apply_filters( 'pre_add_site_option_' . $option, $value );
805 if ( !is_multisite() ) {
806 $result = add_option( $option, $value );
808 $cache_key = "{$wpdb->siteid}:$option";
810 if ( false !== get_site_option( $option ) )
813 $value = sanitize_option( $option, $value );
814 wp_cache_set( $cache_key, $value, 'site-options' );
817 $value = maybe_serialize( $value );
818 $result = $wpdb->insert( $wpdb->sitemeta, array('site_id' => $wpdb->siteid, 'meta_key' => $option, 'meta_value' => $value ) );
823 do_action( "add_site_option_{$option}", $option, $value );
824 do_action( "add_site_option", $option, $value );
831 * Removes site option by name.
833 * @see delete_option()
838 * @uses do_action() Calls 'pre_delete_site_option_$option' hook before option is deleted.
839 * @uses do_action() Calls 'delete_site_option' and 'delete_site_option_$option'
842 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
843 * @return bool True, if succeed. False, if failure.
845 function delete_site_option( $option ) {
848 // ms_protect_special_option( $option ); @todo
850 do_action( 'pre_delete_site_option_' . $option );
852 if ( !is_multisite() ) {
853 $result = delete_option( $option );
855 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );
856 if ( is_null( $row ) || !$row->meta_id )
858 $cache_key = "{$wpdb->siteid}:$option";
859 wp_cache_delete( $cache_key, 'site-options' );
861 $result = $wpdb->delete( $wpdb->sitemeta, array( 'meta_key' => $option, 'site_id' => $wpdb->siteid ) );
865 do_action( "delete_site_option_{$option}", $option );
866 do_action( "delete_site_option", $option );
873 * Update the value of a site option that was already added.
875 * @see update_option()
880 * @uses apply_filters() Calls 'pre_update_site_option_$option' hook to allow overwriting the
881 * option value to be stored.
882 * @uses do_action() Calls 'update_site_option_$option' and 'update_site_option' hooks on success.
884 * @param string $option Name of option. Expected to not be SQL-escaped.
885 * @param mixed $value Option value. Expected to not be SQL-escaped.
886 * @return bool False if value was not updated and true if value was updated.
888 function update_site_option( $option, $value ) {
891 $oldvalue = get_site_option( $option );
892 $value = apply_filters( 'pre_update_site_option_' . $option, $value, $oldvalue );
894 if ( $value === $oldvalue )
897 if ( false === $oldvalue )
898 return add_site_option( $option, $value );
900 if ( !is_multisite() ) {
901 $result = update_option( $option, $value );
903 $value = sanitize_option( $option, $value );
904 $cache_key = "{$wpdb->siteid}:$option";
905 wp_cache_set( $cache_key, $value, 'site-options' );
908 $value = maybe_serialize( $value );
909 $result = $wpdb->update( $wpdb->sitemeta, array( 'meta_value' => $value ), array( 'site_id' => $wpdb->siteid, 'meta_key' => $option ) );
914 do_action( "update_site_option_{$option}", $option, $value, $oldvalue );
915 do_action( "update_site_option", $option, $value, $oldvalue );
922 * Delete a site transient.
926 * @subpackage Transient
928 * @uses do_action() Calls 'delete_site_transient_$transient' hook before transient is deleted.
929 * @uses do_action() Calls 'deleted_site_transient' hook on success.
931 * @param string $transient Transient name. Expected to not be SQL-escaped.
932 * @return bool True if successful, false otherwise
934 function delete_site_transient( $transient ) {
935 global $_wp_using_ext_object_cache;
937 do_action( 'delete_site_transient_' . $transient, $transient );
938 if ( $_wp_using_ext_object_cache ) {
939 $result = wp_cache_delete( $transient, 'site-transient' );
941 $option_timeout = '_site_transient_timeout_' . $transient;
942 $option = '_site_transient_' . $transient;
943 $result = delete_site_option( $option );
945 delete_site_option( $option_timeout );
948 do_action( 'deleted_site_transient', $transient );
953 * Get the value of a site transient.
955 * If the transient does not exist or does not have a value, then the return value
958 * @see get_transient()
961 * @subpackage Transient
963 * @uses apply_filters() Calls 'pre_site_transient_$transient' hook before checking the transient.
964 * Any value other than false will "short-circuit" the retrieval of the transient
965 * and return the returned value.
966 * @uses apply_filters() Calls 'site_transient_$option' hook, after checking the transient, with
967 * the transient value.
969 * @param string $transient Transient name. Expected to not be SQL-escaped.
970 * @return mixed Value of transient
972 function get_site_transient( $transient ) {
973 global $_wp_using_ext_object_cache;
975 $pre = apply_filters( 'pre_site_transient_' . $transient, false );
976 if ( false !== $pre )
979 if ( $_wp_using_ext_object_cache ) {
980 $value = wp_cache_get( $transient, 'site-transient' );
982 // Core transients that do not have a timeout. Listed here so querying timeouts can be avoided.
983 $no_timeout = array('update_core', 'update_plugins', 'update_themes');
984 $transient_option = '_site_transient_' . $transient;
985 if ( ! in_array( $transient, $no_timeout ) ) {
986 $transient_timeout = '_site_transient_timeout_' . $transient;
987 $timeout = get_site_option( $transient_timeout );
988 if ( false !== $timeout && $timeout < time() ) {
989 delete_site_option( $transient_option );
990 delete_site_option( $transient_timeout );
995 $value = get_site_option( $transient_option );
998 return apply_filters( 'site_transient_' . $transient, $value );
1002 * Set/update the value of a site transient.
1004 * You do not need to serialize values, if the value needs to be serialize, then
1005 * it will be serialized before it is set.
1007 * @see set_transient()
1009 * @package WordPress
1010 * @subpackage Transient
1012 * @uses apply_filters() Calls 'pre_set_site_transient_$transient' hook to allow overwriting the
1013 * transient value to be stored.
1014 * @uses do_action() Calls 'set_site_transient_$transient' and 'setted_site_transient' hooks on success.
1016 * @param string $transient Transient name. Expected to not be SQL-escaped.
1017 * @param mixed $value Transient value. Expected to not be SQL-escaped.
1018 * @param int $expiration Time until expiration in seconds, default 0
1019 * @return bool False if value was not set and true if value was set.
1021 function set_site_transient( $transient, $value, $expiration = 0 ) {
1022 global $_wp_using_ext_object_cache;
1024 $value = apply_filters( 'pre_set_site_transient_' . $transient, $value );
1026 if ( $_wp_using_ext_object_cache ) {
1027 $result = wp_cache_set( $transient, $value, 'site-transient', $expiration );
1029 $transient_timeout = '_site_transient_timeout_' . $transient;
1030 $transient = '_site_transient_' . $transient;
1031 if ( false === get_site_option( $transient ) ) {
1033 add_site_option( $transient_timeout, time() + $expiration );
1034 $result = add_site_option( $transient, $value );
1037 update_site_option( $transient_timeout, time() + $expiration );
1038 $result = update_site_option( $transient, $value );
1042 do_action( 'set_site_transient_' . $transient );
1043 do_action( 'setted_site_transient', $transient );