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 $option = trim( $option );
36 if ( empty( $option ) )
39 // Allow plugins to short-circuit options.
40 $pre = apply_filters( 'pre_option_' . $option, false );
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 if ( 'alloptions' === $option || 'notoptions' === $option )
108 wp_die( sprintf( __( '%s is a protected WP option and may not be modified' ), esc_html( $option ) ) );
112 * Print option value after sanitizing for forms.
114 * @uses attr Sanitizes value.
119 * @param string $option Option name.
121 function form_option( $option ) {
122 echo esc_attr( get_option( $option ) );
126 * Loads and caches all autoloaded options, if available or all options.
132 * @return array List of all options.
134 function wp_load_alloptions() {
137 if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
138 $alloptions = wp_cache_get( 'alloptions', 'options' );
142 if ( !$alloptions ) {
143 $suppress = $wpdb->suppress_errors();
144 if ( !$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) )
145 $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
146 $wpdb->suppress_errors($suppress);
147 $alloptions = array();
148 foreach ( (array) $alloptions_db as $o ) {
149 $alloptions[$o->option_name] = $o->option_value;
151 if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
152 wp_cache_add( 'alloptions', $alloptions, 'options' );
159 * Loads and caches certain often requested site options if is_multisite() and a persistent cache is not being used.
165 * @param int $site_id Optional site ID for which to query the options. Defaults to the current site.
167 function wp_load_core_site_options( $site_id = null ) {
168 global $wpdb, $_wp_using_ext_object_cache;
170 if ( !is_multisite() || $_wp_using_ext_object_cache || defined( 'WP_INSTALLING' ) )
173 if ( empty($site_id) )
174 $site_id = $wpdb->siteid;
176 $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', 'ms_files_rewriting' );
178 $core_options_in = "'" . implode("', '", $core_options) . "'";
179 $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) );
181 foreach ( $options as $option ) {
182 $key = $option->meta_key;
183 $cache_key = "{$site_id}:$key";
184 $option->meta_value = maybe_unserialize( $option->meta_value );
186 wp_cache_set( $cache_key, $option->meta_value, 'site-options' );
191 * Update the value of an option that was already added.
193 * You do not need to serialize values. If the value needs to be serialized, then
194 * it will be serialized before it is inserted into the database. Remember,
195 * resources can not be serialized or added as an option.
197 * If the option does not exist, then the option will be added with the option
198 * value, but you will not be able to set whether it is autoloaded. If you want
199 * to set whether an option is autoloaded, then you need to use the add_option().
205 * @uses apply_filters() Calls 'pre_update_option_$option' hook to allow overwriting the
206 * option value to be stored.
207 * @uses do_action() Calls 'update_option' hook before updating the option.
208 * @uses do_action() Calls 'update_option_$option' and 'updated_option' hooks on success.
210 * @param string $option Option name. Expected to not be SQL-escaped.
211 * @param mixed $newvalue Option value. Expected to not be SQL-escaped.
212 * @return bool False if value was not updated and true if value was updated.
214 function update_option( $option, $newvalue ) {
217 $option = trim($option);
218 if ( empty($option) )
221 wp_protect_special_option( $option );
223 if ( is_object($newvalue) )
224 $newvalue = clone $newvalue;
226 $newvalue = sanitize_option( $option, $newvalue );
227 $oldvalue = get_option( $option );
228 $newvalue = apply_filters( 'pre_update_option_' . $option, $newvalue, $oldvalue );
230 // If the new and old values are the same, no need to update.
231 if ( $newvalue === $oldvalue )
234 if ( false === $oldvalue )
235 return add_option( $option, $newvalue );
237 $notoptions = wp_cache_get( 'notoptions', 'options' );
238 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
239 unset( $notoptions[$option] );
240 wp_cache_set( 'notoptions', $notoptions, 'options' );
243 $_newvalue = $newvalue;
244 $newvalue = maybe_serialize( $newvalue );
246 do_action( 'update_option', $option, $oldvalue, $_newvalue );
247 if ( ! defined( 'WP_INSTALLING' ) ) {
248 $alloptions = wp_load_alloptions();
249 if ( isset( $alloptions[$option] ) ) {
250 $alloptions[$option] = $newvalue;
251 wp_cache_set( 'alloptions', $alloptions, 'options' );
253 wp_cache_set( $option, $newvalue, 'options' );
257 $result = $wpdb->update( $wpdb->options, array( 'option_value' => $newvalue ), array( 'option_name' => $option ) );
260 do_action( "update_option_{$option}", $oldvalue, $_newvalue );
261 do_action( 'updated_option', $option, $oldvalue, $_newvalue );
270 * You do not need to serialize values. If the value needs to be serialized, then
271 * it will be serialized before it is inserted into the database. Remember,
272 * resources can not be serialized or added as an option.
274 * You can create options without values and then update the values later.
275 * Existing options will not be updated and checks are performed to ensure that you
276 * aren't adding a protected WordPress option. Care should be taken to not name
277 * options the same as the ones which are protected.
283 * @uses do_action() Calls 'add_option' hook before adding the option.
284 * @uses do_action() Calls 'add_option_$option' and 'added_option' hooks on success.
286 * @param string $option Name of option to add. Expected to not be SQL-escaped.
287 * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
288 * @param mixed $deprecated Optional. Description. Not used anymore.
289 * @param bool $autoload Optional. Default is enabled. Whether to load the option when WordPress starts up.
290 * @return bool False if option was not added and true if option was added.
292 function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) {
295 if ( !empty( $deprecated ) )
296 _deprecated_argument( __FUNCTION__, '2.3' );
298 $option = trim($option);
299 if ( empty($option) )
302 wp_protect_special_option( $option );
304 if ( is_object($value) )
305 $value = clone $value;
307 $value = sanitize_option( $option, $value );
309 // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
310 $notoptions = wp_cache_get( 'notoptions', 'options' );
311 if ( !is_array( $notoptions ) || !isset( $notoptions[$option] ) )
312 if ( false !== get_option( $option ) )
316 $value = maybe_serialize( $value );
317 $autoload = ( 'no' === $autoload ) ? 'no' : 'yes';
318 do_action( 'add_option', $option, $_value );
319 if ( ! defined( 'WP_INSTALLING' ) ) {
320 if ( 'yes' == $autoload ) {
321 $alloptions = wp_load_alloptions();
322 $alloptions[$option] = $value;
323 wp_cache_set( 'alloptions', $alloptions, 'options' );
325 wp_cache_set( $option, $value, 'options' );
329 // This option exists now
330 $notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh
331 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
332 unset( $notoptions[$option] );
333 wp_cache_set( 'notoptions', $notoptions, 'options' );
336 $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 ) );
339 do_action( "add_option_{$option}", $option, $_value );
340 do_action( 'added_option', $option, $_value );
347 * Removes option by name. Prevents removal of protected WordPress options.
353 * @uses do_action() Calls 'delete_option' hook before option is deleted.
354 * @uses do_action() Calls 'deleted_option' and 'delete_option_$option' hooks on success.
356 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
357 * @return bool True, if option is successfully deleted. False on failure.
359 function delete_option( $option ) {
362 $option = trim( $option );
363 if ( empty( $option ) )
366 wp_protect_special_option( $option );
368 // Get the ID, if no ID then return
369 $row = $wpdb->get_row( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) );
370 if ( is_null( $row ) )
372 do_action( 'delete_option', $option );
373 $result = $wpdb->delete( $wpdb->options, array( 'option_name' => $option ) );
374 if ( ! defined( 'WP_INSTALLING' ) ) {
375 if ( 'yes' == $row->autoload ) {
376 $alloptions = wp_load_alloptions();
377 if ( is_array( $alloptions ) && isset( $alloptions[$option] ) ) {
378 unset( $alloptions[$option] );
379 wp_cache_set( 'alloptions', $alloptions, 'options' );
382 wp_cache_delete( $option, 'options' );
386 do_action( "delete_option_$option", $option );
387 do_action( 'deleted_option', $option );
394 * Delete a transient.
398 * @subpackage Transient
400 * @uses do_action() Calls 'delete_transient_$transient' hook before transient is deleted.
401 * @uses do_action() Calls 'deleted_transient' hook on success.
403 * @param string $transient Transient name. Expected to not be SQL-escaped.
404 * @return bool true if successful, false otherwise
406 function delete_transient( $transient ) {
407 global $_wp_using_ext_object_cache;
409 do_action( 'delete_transient_' . $transient, $transient );
411 if ( $_wp_using_ext_object_cache ) {
412 $result = wp_cache_delete( $transient, 'transient' );
414 $option_timeout = '_transient_timeout_' . $transient;
415 $option = '_transient_' . $transient;
416 $result = delete_option( $option );
418 delete_option( $option_timeout );
422 do_action( 'deleted_transient', $transient );
427 * Get the value of a transient.
429 * If the transient does not exist or does not have a value, then the return value
432 * @uses apply_filters() Calls 'pre_transient_$transient' hook before checking the transient.
433 * Any value other than false will "short-circuit" the retrieval of the transient
434 * and return the returned value.
435 * @uses apply_filters() Calls 'transient_$option' hook, after checking the transient, with
436 * the transient value.
440 * @subpackage Transient
442 * @param string $transient Transient name. Expected to not be SQL-escaped
443 * @return mixed Value of transient
445 function get_transient( $transient ) {
446 global $_wp_using_ext_object_cache;
448 $pre = apply_filters( 'pre_transient_' . $transient, false );
449 if ( false !== $pre )
452 if ( $_wp_using_ext_object_cache ) {
453 $value = wp_cache_get( $transient, 'transient' );
455 $transient_option = '_transient_' . $transient;
456 if ( ! defined( 'WP_INSTALLING' ) ) {
457 // If option is not in alloptions, it is not autoloaded and thus has a timeout
458 $alloptions = wp_load_alloptions();
459 if ( !isset( $alloptions[$transient_option] ) ) {
460 $transient_timeout = '_transient_timeout_' . $transient;
461 if ( get_option( $transient_timeout ) < time() ) {
462 delete_option( $transient_option );
463 delete_option( $transient_timeout );
469 $value = get_option( $transient_option );
472 return apply_filters( 'transient_' . $transient, $value );
476 * Set/update the value of a transient.
478 * You do not need to serialize values. If the value needs to be serialized, then
479 * it will be serialized before it is set.
483 * @subpackage Transient
485 * @uses apply_filters() Calls 'pre_set_transient_$transient' hook to allow overwriting the
486 * transient value to be stored.
487 * @uses do_action() Calls 'set_transient_$transient' and 'setted_transient' hooks on success.
489 * @param string $transient Transient name. Expected to not be SQL-escaped.
490 * @param mixed $value Transient value. Expected to not be SQL-escaped.
491 * @param int $expiration Time until expiration in seconds, default 0
492 * @return bool False if value was not set and true if value was set.
494 function set_transient( $transient, $value, $expiration = 0 ) {
495 global $_wp_using_ext_object_cache;
497 $value = apply_filters( 'pre_set_transient_' . $transient, $value );
499 if ( $_wp_using_ext_object_cache ) {
500 $result = wp_cache_set( $transient, $value, 'transient', $expiration );
502 $transient_timeout = '_transient_timeout_' . $transient;
503 $transient = '_transient_' . $transient;
504 if ( false === get_option( $transient ) ) {
508 add_option( $transient_timeout, time() + $expiration, '', 'no' );
510 $result = add_option( $transient, $value, '', $autoload );
513 update_option( $transient_timeout, time() + $expiration );
514 $result = update_option( $transient, $value );
518 do_action( 'set_transient_' . $transient, $value, $expiration );
519 do_action( 'setted_transient', $transient, $value, $expiration );
525 * Saves and restores user interface settings stored in a cookie.
527 * Checks if the current user-settings cookie is updated and stores it. When no
528 * cookie exists (different browser used), adds the last saved cookie restoring
535 function wp_user_settings() {
540 if ( defined('DOING_AJAX') )
543 if ( ! $user = wp_get_current_user() )
546 if ( is_super_admin( $user->ID ) &&
547 ! in_array( get_current_blog_id(), array_keys( get_blogs_of_user( $user->ID ) ) )
551 $settings = get_user_option( 'user-settings', $user->ID );
553 if ( isset( $_COOKIE['wp-settings-' . $user->ID] ) ) {
554 $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user->ID] );
556 if ( ! empty( $cookie ) && strpos( $cookie, '=' ) ) {
557 if ( $cookie == $settings )
560 $last_time = (int) get_user_option( 'user-settings-time', $user->ID );
561 $saved = isset( $_COOKIE['wp-settings-time-' . $user->ID]) ? preg_replace( '/[^0-9]/', '', $_COOKIE['wp-settings-time-' . $user->ID] ) : 0;
563 if ( $saved > $last_time ) {
564 update_user_option( $user->ID, 'user-settings', $cookie, false );
565 update_user_option( $user->ID, 'user-settings-time', time() - 5, false );
571 setcookie( 'wp-settings-' . $user->ID, $settings, time() + YEAR_IN_SECONDS, SITECOOKIEPATH );
572 setcookie( 'wp-settings-time-' . $user->ID, time(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH );
573 $_COOKIE['wp-settings-' . $user->ID] = $settings;
577 * Retrieve user interface setting value based on setting name.
583 * @param string $name The name of the setting.
584 * @param string $default Optional default value to return when $name is not set.
585 * @return mixed the last saved user setting or the default value/false if it doesn't exist.
587 function get_user_setting( $name, $default = false ) {
589 $all = get_all_user_settings();
591 return isset($all[$name]) ? $all[$name] : $default;
595 * Add or update user interface setting.
597 * Both $name and $value can contain only ASCII letters, numbers and underscores.
598 * This function has to be used before any output has started as it calls setcookie().
604 * @param string $name The name of the setting.
605 * @param string $value The value for the setting.
606 * @return bool true if set successfully/false if not.
608 function set_user_setting( $name, $value ) {
610 if ( headers_sent() )
613 $all = get_all_user_settings();
614 $name = preg_replace( '/[^A-Za-z0-9_]+/', '', $name );
619 $all[$name] = $value;
621 return wp_set_all_user_settings($all);
625 * Delete user interface settings.
627 * Deleting settings would reset them to the defaults.
628 * This function has to be used before any output has started as it calls setcookie().
634 * @param mixed $names The name or array of names of the setting to be deleted.
635 * @return bool true if deleted successfully/false if not.
637 function delete_user_setting( $names ) {
639 if ( headers_sent() )
642 $all = get_all_user_settings();
643 $names = (array) $names;
645 foreach ( $names as $name ) {
646 if ( isset($all[$name]) ) {
652 if ( isset($deleted) )
653 return wp_set_all_user_settings($all);
659 * Retrieve all user interface settings.
665 * @return array the last saved user settings or empty array.
667 function get_all_user_settings() {
668 global $_updated_user_settings;
670 if ( ! $user = wp_get_current_user() )
673 if ( isset($_updated_user_settings) && is_array($_updated_user_settings) )
674 return $_updated_user_settings;
677 if ( isset($_COOKIE['wp-settings-' . $user->ID]) ) {
678 $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user->ID] );
680 if ( $cookie && strpos($cookie, '=') ) // the '=' cannot be 1st char
681 parse_str($cookie, $all);
684 $option = get_user_option('user-settings', $user->ID);
685 if ( $option && is_string($option) )
686 parse_str( $option, $all );
693 * Private. Set all user interface settings.
699 * @param unknown $all
702 function wp_set_all_user_settings($all) {
703 global $_updated_user_settings;
705 if ( ! $user = wp_get_current_user() )
708 if ( is_super_admin( $user->ID ) &&
709 ! in_array( get_current_blog_id(), array_keys( get_blogs_of_user( $user->ID ) ) )
713 $_updated_user_settings = $all;
715 foreach ( $all as $k => $v ) {
716 $v = preg_replace( '/[^A-Za-z0-9_]+/', '', $v );
717 $settings .= $k . '=' . $v . '&';
720 $settings = rtrim($settings, '&');
722 update_user_option( $user->ID, 'user-settings', $settings, false );
723 update_user_option( $user->ID, 'user-settings-time', time(), false );
729 * Delete the user settings of the current user.
735 function delete_all_user_settings() {
736 if ( ! $user = wp_get_current_user() )
739 update_user_option( $user->ID, 'user-settings', '', false );
740 setcookie('wp-settings-' . $user->ID, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH);
744 * Retrieve site option value based on name of option.
751 * @uses apply_filters() Calls 'pre_site_option_$option' before checking the option.
752 * Any value other than false will "short-circuit" the retrieval of the option
753 * and return the returned value.
754 * @uses apply_filters() Calls 'site_option_$option', after checking the option, with
757 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
758 * @param mixed $default Optional value to return if option doesn't exist. Default false.
759 * @param bool $use_cache Whether to use cache. Multisite only. Default true.
760 * @return mixed Value set for the option.
762 function get_site_option( $option, $default = false, $use_cache = true ) {
765 // Allow plugins to short-circuit site options.
766 $pre = apply_filters( 'pre_site_option_' . $option, false );
767 if ( false !== $pre )
770 if ( ! is_multisite() ) {
771 $default = apply_filters( 'default_site_option_' . $option, $default );
772 $value = get_option($option, $default);
774 $cache_key = "{$wpdb->siteid}:$option";
776 $value = wp_cache_get($cache_key, 'site-options');
778 if ( !isset($value) || (false === $value) ) {
779 $row = $wpdb->get_row( $wpdb->prepare("SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );
781 // Has to be get_row instead of get_var because of funkiness with 0, false, null values
782 if ( is_object( $row ) ) {
783 $value = $row->meta_value;
784 $value = maybe_unserialize( $value );
785 wp_cache_set( $cache_key, $value, 'site-options' );
787 $value = apply_filters( 'default_site_option_' . $option, $default );
792 return apply_filters( 'site_option_' . $option, $value );
796 * Add a new site option.
798 * Existing options will not be updated. Note that prior to 3.3 this wasn't the case.
805 * @uses apply_filters() Calls 'pre_add_site_option_$option' hook to allow overwriting the
806 * option value to be stored.
807 * @uses do_action() Calls 'add_site_option_$option' and 'add_site_option' hooks on success.
809 * @param string $option Name of option to add. Expected to not be SQL-escaped.
810 * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
811 * @return bool False if option was not added and true if option was added.
813 function add_site_option( $option, $value ) {
816 $value = apply_filters( 'pre_add_site_option_' . $option, $value );
818 if ( !is_multisite() ) {
819 $result = add_option( $option, $value );
821 $cache_key = "{$wpdb->siteid}:$option";
823 if ( false !== get_site_option( $option ) )
826 $value = sanitize_option( $option, $value );
827 wp_cache_set( $cache_key, $value, 'site-options' );
830 $value = maybe_serialize( $value );
831 $result = $wpdb->insert( $wpdb->sitemeta, array('site_id' => $wpdb->siteid, 'meta_key' => $option, 'meta_value' => $value ) );
836 do_action( "add_site_option_{$option}", $option, $value );
837 do_action( "add_site_option", $option, $value );
844 * Removes site option by name.
846 * @see delete_option()
851 * @uses do_action() Calls 'pre_delete_site_option_$option' hook before option is deleted.
852 * @uses do_action() Calls 'delete_site_option' and 'delete_site_option_$option'
855 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
856 * @return bool True, if succeed. False, if failure.
858 function delete_site_option( $option ) {
861 // ms_protect_special_option( $option ); @todo
863 do_action( 'pre_delete_site_option_' . $option );
865 if ( !is_multisite() ) {
866 $result = delete_option( $option );
868 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );
869 if ( is_null( $row ) || !$row->meta_id )
871 $cache_key = "{$wpdb->siteid}:$option";
872 wp_cache_delete( $cache_key, 'site-options' );
874 $result = $wpdb->delete( $wpdb->sitemeta, array( 'meta_key' => $option, 'site_id' => $wpdb->siteid ) );
878 do_action( "delete_site_option_{$option}", $option );
879 do_action( "delete_site_option", $option );
886 * Update the value of a site option that was already added.
888 * @see update_option()
893 * @uses apply_filters() Calls 'pre_update_site_option_$option' hook to allow overwriting the
894 * option value to be stored.
895 * @uses do_action() Calls 'update_site_option_$option' and 'update_site_option' hooks on success.
897 * @param string $option Name of option. Expected to not be SQL-escaped.
898 * @param mixed $value Option value. Expected to not be SQL-escaped.
899 * @return bool False if value was not updated and true if value was updated.
901 function update_site_option( $option, $value ) {
904 $oldvalue = get_site_option( $option );
905 $value = apply_filters( 'pre_update_site_option_' . $option, $value, $oldvalue );
907 if ( $value === $oldvalue )
910 if ( false === $oldvalue )
911 return add_site_option( $option, $value );
913 if ( !is_multisite() ) {
914 $result = update_option( $option, $value );
916 $value = sanitize_option( $option, $value );
917 $cache_key = "{$wpdb->siteid}:$option";
918 wp_cache_set( $cache_key, $value, 'site-options' );
921 $value = maybe_serialize( $value );
922 $result = $wpdb->update( $wpdb->sitemeta, array( 'meta_value' => $value ), array( 'site_id' => $wpdb->siteid, 'meta_key' => $option ) );
927 do_action( "update_site_option_{$option}", $option, $value, $oldvalue );
928 do_action( "update_site_option", $option, $value, $oldvalue );
935 * Delete a site transient.
939 * @subpackage Transient
941 * @uses do_action() Calls 'delete_site_transient_$transient' hook before transient is deleted.
942 * @uses do_action() Calls 'deleted_site_transient' hook on success.
944 * @param string $transient Transient name. Expected to not be SQL-escaped.
945 * @return bool True if successful, false otherwise
947 function delete_site_transient( $transient ) {
948 global $_wp_using_ext_object_cache;
950 do_action( 'delete_site_transient_' . $transient, $transient );
951 if ( $_wp_using_ext_object_cache ) {
952 $result = wp_cache_delete( $transient, 'site-transient' );
954 $option_timeout = '_site_transient_timeout_' . $transient;
955 $option = '_site_transient_' . $transient;
956 $result = delete_site_option( $option );
958 delete_site_option( $option_timeout );
961 do_action( 'deleted_site_transient', $transient );
966 * Get the value of a site transient.
968 * If the transient does not exist or does not have a value, then the return value
971 * @see get_transient()
974 * @subpackage Transient
976 * @uses apply_filters() Calls 'pre_site_transient_$transient' hook before checking the transient.
977 * Any value other than false will "short-circuit" the retrieval of the transient
978 * and return the returned value.
979 * @uses apply_filters() Calls 'site_transient_$option' hook, after checking the transient, with
980 * the transient value.
982 * @param string $transient Transient name. Expected to not be SQL-escaped.
983 * @return mixed Value of transient
985 function get_site_transient( $transient ) {
986 global $_wp_using_ext_object_cache;
988 $pre = apply_filters( 'pre_site_transient_' . $transient, false );
989 if ( false !== $pre )
992 if ( $_wp_using_ext_object_cache ) {
993 $value = wp_cache_get( $transient, 'site-transient' );
995 // Core transients that do not have a timeout. Listed here so querying timeouts can be avoided.
996 $no_timeout = array('update_core', 'update_plugins', 'update_themes');
997 $transient_option = '_site_transient_' . $transient;
998 if ( ! in_array( $transient, $no_timeout ) ) {
999 $transient_timeout = '_site_transient_timeout_' . $transient;
1000 $timeout = get_site_option( $transient_timeout );
1001 if ( false !== $timeout && $timeout < time() ) {
1002 delete_site_option( $transient_option );
1003 delete_site_option( $transient_timeout );
1008 $value = get_site_option( $transient_option );
1011 return apply_filters( 'site_transient_' . $transient, $value );
1015 * Set/update the value of a site transient.
1017 * You do not need to serialize values, if the value needs to be serialize, then
1018 * it will be serialized before it is set.
1020 * @see set_transient()
1022 * @package WordPress
1023 * @subpackage Transient
1025 * @uses apply_filters() Calls 'pre_set_site_transient_$transient' hook to allow overwriting the
1026 * transient value to be stored.
1027 * @uses do_action() Calls 'set_site_transient_$transient' and 'setted_site_transient' hooks on success.
1029 * @param string $transient Transient name. Expected to not be SQL-escaped.
1030 * @param mixed $value Transient value. Expected to not be SQL-escaped.
1031 * @param int $expiration Time until expiration in seconds, default 0
1032 * @return bool False if value was not set and true if value was set.
1034 function set_site_transient( $transient, $value, $expiration = 0 ) {
1035 global $_wp_using_ext_object_cache;
1037 $value = apply_filters( 'pre_set_site_transient_' . $transient, $value );
1039 if ( $_wp_using_ext_object_cache ) {
1040 $result = wp_cache_set( $transient, $value, 'site-transient', $expiration );
1042 $transient_timeout = '_site_transient_timeout_' . $transient;
1043 $transient = '_site_transient_' . $transient;
1044 if ( false === get_site_option( $transient ) ) {
1046 add_site_option( $transient_timeout, time() + $expiration );
1047 $result = add_site_option( $transient, $value );
1050 update_site_option( $transient_timeout, time() + $expiration );
1051 $result = update_site_option( $transient, $value );
1055 do_action( 'set_site_transient_' . $transient, $value, $expiration );
1056 do_action( 'setted_site_transient', $transient, $value, $expiration );