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 ) {
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 $value Option value. Must be serializable if non-scalar. 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, $value ) {
217 $option = trim($option);
218 if ( empty($option) )
221 wp_protect_special_option( $option );
223 if ( is_object( $value ) )
224 $value = clone $value;
226 $value = sanitize_option( $option, $value );
227 $old_value = get_option( $option );
228 $value = apply_filters( 'pre_update_option_' . $option, $value, $old_value );
230 // If the new and old values are the same, no need to update.
231 if ( $value === $old_value )
234 if ( false === $old_value )
235 return add_option( $option, $value );
237 $serialized_value = maybe_serialize( $value );
239 $result = $wpdb->update( $wpdb->options, array( 'option_value' => $serialized_value ), array( 'option_name' => $option ) );
243 $notoptions = wp_cache_get( 'notoptions', 'options' );
244 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
245 unset( $notoptions[$option] );
246 wp_cache_set( 'notoptions', $notoptions, 'options' );
249 do_action( 'update_option', $option, $old_value, $value );
250 if ( ! defined( 'WP_INSTALLING' ) ) {
251 $alloptions = wp_load_alloptions();
252 if ( isset( $alloptions[$option] ) ) {
253 $alloptions[ $option ] = $serialized_value;
254 wp_cache_set( 'alloptions', $alloptions, 'options' );
256 wp_cache_set( $option, $serialized_value, 'options' );
260 do_action( "update_option_{$option}", $old_value, $value );
261 do_action( 'updated_option', $option, $old_value, $value );
268 * You do not need to serialize values. If the value needs to be serialized, then
269 * it will be serialized before it is inserted into the database. Remember,
270 * resources can not be serialized or added as an option.
272 * You can create options without values and then update the values later.
273 * Existing options will not be updated and checks are performed to ensure that you
274 * aren't adding a protected WordPress option. Care should be taken to not name
275 * options the same as the ones which are protected.
281 * @uses do_action() Calls 'add_option' hook before adding the option.
282 * @uses do_action() Calls 'add_option_$option' and 'added_option' hooks on success.
284 * @param string $option Name of option to add. Expected to not be SQL-escaped.
285 * @param mixed $value Optional. Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
286 * @param mixed $deprecated Optional. Description. Not used anymore.
287 * @param bool $autoload Optional. Default is enabled. Whether to load the option when WordPress starts up.
288 * @return bool False if option was not added and true if option was added.
290 function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) {
293 if ( !empty( $deprecated ) )
294 _deprecated_argument( __FUNCTION__, '2.3' );
296 $option = trim($option);
297 if ( empty($option) )
300 wp_protect_special_option( $option );
302 if ( is_object($value) )
303 $value = clone $value;
305 $value = sanitize_option( $option, $value );
307 // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
308 $notoptions = wp_cache_get( 'notoptions', 'options' );
309 if ( !is_array( $notoptions ) || !isset( $notoptions[$option] ) )
310 if ( false !== get_option( $option ) )
313 $serialized_value = maybe_serialize( $value );
314 $autoload = ( 'no' === $autoload ) ? 'no' : 'yes';
315 do_action( 'add_option', $option, $value );
317 $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, $serialized_value, $autoload ) );
321 if ( ! defined( 'WP_INSTALLING' ) ) {
322 if ( 'yes' == $autoload ) {
323 $alloptions = wp_load_alloptions();
324 $alloptions[ $option ] = $serialized_value;
325 wp_cache_set( 'alloptions', $alloptions, 'options' );
327 wp_cache_set( $option, $serialized_value, 'options' );
331 // This option exists now
332 $notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh
333 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
334 unset( $notoptions[$option] );
335 wp_cache_set( 'notoptions', $notoptions, 'options' );
338 do_action( "add_option_{$option}", $option, $value );
339 do_action( 'added_option', $option, $value );
344 * Removes option by name. Prevents removal of protected WordPress options.
350 * @uses do_action() Calls 'delete_option' hook before option is deleted.
351 * @uses do_action() Calls 'deleted_option' and 'delete_option_$option' hooks on success.
353 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
354 * @return bool True, if option is successfully deleted. False on failure.
356 function delete_option( $option ) {
359 $option = trim( $option );
360 if ( empty( $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 do_action( 'delete_transient_' . $transient, $transient );
406 if ( wp_using_ext_object_cache() ) {
407 $result = wp_cache_delete( $transient, 'transient' );
409 $option_timeout = '_transient_timeout_' . $transient;
410 $option = '_transient_' . $transient;
411 $result = delete_option( $option );
413 delete_option( $option_timeout );
417 do_action( 'deleted_transient', $transient );
422 * Get the value of a transient.
424 * If the transient does not exist or does not have a value, then the return value
427 * @uses apply_filters() Calls 'pre_transient_$transient' hook before checking the transient.
428 * Any value other than false will "short-circuit" the retrieval of the transient
429 * and return the returned value.
430 * @uses apply_filters() Calls 'transient_$option' hook, after checking the transient, with
431 * the transient value.
435 * @subpackage Transient
437 * @param string $transient Transient name. Expected to not be SQL-escaped
438 * @return mixed Value of transient
440 function get_transient( $transient ) {
441 $pre = apply_filters( 'pre_transient_' . $transient, false );
442 if ( false !== $pre )
445 if ( wp_using_ext_object_cache() ) {
446 $value = wp_cache_get( $transient, 'transient' );
448 $transient_option = '_transient_' . $transient;
449 if ( ! defined( 'WP_INSTALLING' ) ) {
450 // If option is not in alloptions, it is not autoloaded and thus has a timeout
451 $alloptions = wp_load_alloptions();
452 if ( !isset( $alloptions[$transient_option] ) ) {
453 $transient_timeout = '_transient_timeout_' . $transient;
454 if ( get_option( $transient_timeout ) < time() ) {
455 delete_option( $transient_option );
456 delete_option( $transient_timeout );
462 if ( ! isset( $value ) )
463 $value = get_option( $transient_option );
466 return apply_filters( 'transient_' . $transient, $value );
470 * Set/update the value of a transient.
472 * You do not need to serialize values. If the value needs to be serialized, then
473 * it will be serialized before it is set.
477 * @subpackage Transient
479 * @uses apply_filters() Calls 'pre_set_transient_$transient' hook to allow overwriting the
480 * transient value to be stored.
481 * @uses do_action() Calls 'set_transient_$transient' and 'setted_transient' hooks on success.
483 * @param string $transient Transient name. Expected to not be SQL-escaped.
484 * @param mixed $value Transient value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
485 * @param int $expiration Time until expiration in seconds, default 0
486 * @return bool False if value was not set and true if value was set.
488 function set_transient( $transient, $value, $expiration = 0 ) {
489 $value = apply_filters( 'pre_set_transient_' . $transient, $value );
490 $expiration = (int) $expiration;
492 if ( wp_using_ext_object_cache() ) {
493 $result = wp_cache_set( $transient, $value, 'transient', $expiration );
495 $transient_timeout = '_transient_timeout_' . $transient;
496 $transient = '_transient_' . $transient;
497 if ( false === get_option( $transient ) ) {
501 add_option( $transient_timeout, time() + $expiration, '', 'no' );
503 $result = add_option( $transient, $value, '', $autoload );
506 update_option( $transient_timeout, time() + $expiration );
507 $result = update_option( $transient, $value );
511 do_action( 'set_transient_' . $transient, $value, $expiration );
512 do_action( 'setted_transient', $transient, $value, $expiration );
518 * Saves and restores user interface settings stored in a cookie.
520 * Checks if the current user-settings cookie is updated and stores it. When no
521 * cookie exists (different browser used), adds the last saved cookie restoring
528 function wp_user_settings() {
533 if ( defined('DOING_AJAX') )
536 if ( ! $user_id = get_current_user_id() )
539 if ( is_super_admin() && ! is_user_member_of_blog() )
542 $settings = (string) get_user_option( 'user-settings', $user_id );
544 if ( isset( $_COOKIE['wp-settings-' . $user_id] ) ) {
545 $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user_id] );
547 // No change or both empty
548 if ( $cookie == $settings )
551 $last_saved = (int) get_user_option( 'user-settings-time', $user_id );
552 $current = isset( $_COOKIE['wp-settings-time-' . $user_id]) ? preg_replace( '/[^0-9]/', '', $_COOKIE['wp-settings-time-' . $user_id] ) : 0;
554 // The cookie is newer than the saved value. Update the user_option and leave the cookie as-is
555 if ( $current > $last_saved ) {
556 update_user_option( $user_id, 'user-settings', $cookie, false );
557 update_user_option( $user_id, 'user-settings-time', time() - 5, false );
562 // The cookie is not set in the current browser or the saved value is newer.
563 setcookie( 'wp-settings-' . $user_id, $settings, time() + YEAR_IN_SECONDS, SITECOOKIEPATH );
564 setcookie( 'wp-settings-time-' . $user_id, time(), time() + YEAR_IN_SECONDS, 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 ) {
580 $all_user_settings = get_all_user_settings();
582 return isset( $all_user_settings[$name] ) ? $all_user_settings[$name] : $default;
586 * Add or update user interface setting.
588 * Both $name and $value can contain only ASCII letters, numbers and underscores.
589 * This function has to be used before any output has started as it calls setcookie().
595 * @param string $name The name of the setting.
596 * @param string $value The value for the setting.
597 * @return bool true if set successfully/false if not.
599 function set_user_setting( $name, $value ) {
601 if ( headers_sent() )
604 $all_user_settings = get_all_user_settings();
605 $all_user_settings[$name] = $value;
607 return wp_set_all_user_settings( $all_user_settings );
611 * Delete user interface settings.
613 * Deleting settings would reset them to the defaults.
614 * This function has to be used before any output has started as it calls setcookie().
620 * @param mixed $names The name or array of names of the setting to be deleted.
621 * @return bool true if deleted successfully/false if not.
623 function delete_user_setting( $names ) {
625 if ( headers_sent() )
628 $all_user_settings = get_all_user_settings();
629 $names = (array) $names;
632 foreach ( $names as $name ) {
633 if ( isset( $all_user_settings[$name] ) ) {
634 unset( $all_user_settings[$name] );
640 return wp_set_all_user_settings( $all_user_settings );
646 * Retrieve all user interface settings.
652 * @return array the last saved user settings or empty array.
654 function get_all_user_settings() {
655 global $_updated_user_settings;
657 if ( ! $user_id = get_current_user_id() )
660 if ( isset( $_updated_user_settings ) && is_array( $_updated_user_settings ) )
661 return $_updated_user_settings;
663 $user_settings = array();
664 if ( isset( $_COOKIE['wp-settings-' . $user_id] ) ) {
665 $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user_id] );
667 if ( $cookie && strpos( $cookie, '=' ) ) // '=' cannot be 1st char
668 parse_str( $cookie, $user_settings );
671 $option = get_user_option( 'user-settings', $user_id );
672 if ( $option && is_string($option) )
673 parse_str( $option, $user_settings );
676 $_updated_user_settings = $user_settings;
677 return $user_settings;
681 * Private. Set all user interface settings.
687 * @param array $user_settings
690 function wp_set_all_user_settings( $user_settings ) {
691 global $_updated_user_settings;
693 if ( ! $user_id = get_current_user_id() )
696 if ( is_super_admin() && ! is_user_member_of_blog() )
700 foreach ( $user_settings as $name => $value ) {
701 $_name = preg_replace( '/[^A-Za-z0-9_]+/', '', $name );
702 $_value = preg_replace( '/[^A-Za-z0-9_]+/', '', $value );
704 if ( ! empty( $_name ) )
705 $settings .= $_name . '=' . $_value . '&';
708 $settings = rtrim($settings, '&');
709 parse_str( $settings, $_updated_user_settings );
711 update_user_option( $user_id, 'user-settings', $settings, false );
712 update_user_option( $user_id, 'user-settings-time', time(), false );
718 * Delete the user settings of the current user.
724 function delete_all_user_settings() {
725 if ( ! $user_id = get_current_user_id() )
728 update_user_option( $user_id, 'user-settings', '', false );
729 setcookie('wp-settings-' . $user_id, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH);
733 * Retrieve site option value based on name of option.
740 * @uses apply_filters() Calls 'pre_site_option_$option' before checking the option.
741 * Any value other than false will "short-circuit" the retrieval of the option
742 * and return the returned value.
743 * @uses apply_filters() Calls 'site_option_$option', after checking the option, with
746 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
747 * @param mixed $default Optional value to return if option doesn't exist. Default false.
748 * @param bool $use_cache Whether to use cache. Multisite only. Default true.
749 * @return mixed Value set for the option.
751 function get_site_option( $option, $default = false, $use_cache = true ) {
754 // Allow plugins to short-circuit site options.
755 $pre = apply_filters( 'pre_site_option_' . $option, false );
756 if ( false !== $pre )
759 // prevent non-existent options from triggering multiple queries
760 $notoptions = wp_cache_get( 'notoptions', 'site-options' );
761 if ( isset( $notoptions[$option] ) )
762 return apply_filters( 'default_site_option_' . $option, $default );
764 if ( ! is_multisite() ) {
765 $default = apply_filters( 'default_site_option_' . $option, $default );
766 $value = get_option($option, $default);
768 $cache_key = "{$wpdb->siteid}:$option";
770 $value = wp_cache_get($cache_key, 'site-options');
772 if ( !isset($value) || (false === $value) ) {
773 $row = $wpdb->get_row( $wpdb->prepare("SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );
775 // Has to be get_row instead of get_var because of funkiness with 0, false, null values
776 if ( is_object( $row ) ) {
777 $value = $row->meta_value;
778 $value = maybe_unserialize( $value );
779 wp_cache_set( $cache_key, $value, 'site-options' );
781 $notoptions[$option] = true;
782 wp_cache_set( 'notoptions', $notoptions, 'site-options' );
783 $value = apply_filters( 'default_site_option_' . $option, $default );
788 return apply_filters( 'site_option_' . $option, $value );
792 * Add a new site option.
794 * Existing options will not be updated. Note that prior to 3.3 this wasn't the case.
801 * @uses apply_filters() Calls 'pre_add_site_option_$option' hook to allow overwriting the
802 * option value to be stored.
803 * @uses do_action() Calls 'add_site_option_$option' and 'add_site_option' hooks on success.
805 * @param string $option Name of option to add. Expected to not be SQL-escaped.
806 * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
807 * @return bool False if option was not added and true if option was added.
809 function add_site_option( $option, $value ) {
812 wp_protect_special_option( $option );
814 $value = apply_filters( 'pre_add_site_option_' . $option, $value );
816 if ( !is_multisite() ) {
817 $result = add_option( $option, $value );
819 $cache_key = "{$wpdb->siteid}:$option";
821 // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
822 $notoptions = wp_cache_get( 'notoptions', 'site-options' );
823 if ( ! is_array( $notoptions ) || ! isset( $notoptions[$option] ) )
824 if ( false !== get_site_option( $option ) )
827 $value = sanitize_option( $option, $value );
829 $serialized_value = maybe_serialize( $value );
830 $result = $wpdb->insert( $wpdb->sitemeta, array('site_id' => $wpdb->siteid, 'meta_key' => $option, 'meta_value' => $serialized_value ) );
835 wp_cache_set( $cache_key, $value, 'site-options' );
837 // This option exists now
838 $notoptions = wp_cache_get( 'notoptions', 'site-options' ); // yes, again... we need it to be fresh
839 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
840 unset( $notoptions[$option] );
841 wp_cache_set( 'notoptions', $notoptions, 'site-options' );
846 do_action( "add_site_option_{$option}", $option, $value );
847 do_action( "add_site_option", $option, $value );
854 * Removes site option by name.
856 * @see delete_option()
861 * @uses do_action() Calls 'pre_delete_site_option_$option' hook before option is deleted.
862 * @uses do_action() Calls 'delete_site_option' and 'delete_site_option_$option'
865 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
866 * @return bool True, if succeed. False, if failure.
868 function delete_site_option( $option ) {
871 // ms_protect_special_option( $option ); @todo
873 do_action( 'pre_delete_site_option_' . $option );
875 if ( !is_multisite() ) {
876 $result = delete_option( $option );
878 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );
879 if ( is_null( $row ) || !$row->meta_id )
881 $cache_key = "{$wpdb->siteid}:$option";
882 wp_cache_delete( $cache_key, 'site-options' );
884 $result = $wpdb->delete( $wpdb->sitemeta, array( 'meta_key' => $option, 'site_id' => $wpdb->siteid ) );
888 do_action( "delete_site_option_{$option}", $option );
889 do_action( "delete_site_option", $option );
896 * Update the value of a site option that was already added.
898 * @see update_option()
903 * @uses apply_filters() Calls 'pre_update_site_option_$option' hook to allow overwriting the
904 * option value to be stored.
905 * @uses do_action() Calls 'update_site_option_$option' and 'update_site_option' hooks on success.
907 * @param string $option Name of option. Expected to not be SQL-escaped.
908 * @param mixed $value Option value. Expected to not be SQL-escaped.
909 * @return bool False if value was not updated and true if value was updated.
911 function update_site_option( $option, $value ) {
914 wp_protect_special_option( $option );
916 $old_value = get_site_option( $option );
917 $value = apply_filters( 'pre_update_site_option_' . $option, $value, $old_value );
919 if ( $value === $old_value )
922 if ( false === $old_value )
923 return add_site_option( $option, $value );
925 $notoptions = wp_cache_get( 'notoptions', 'site-options' );
926 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
927 unset( $notoptions[$option] );
928 wp_cache_set( 'notoptions', $notoptions, 'site-options' );
931 if ( !is_multisite() ) {
932 $result = update_option( $option, $value );
934 $value = sanitize_option( $option, $value );
936 $serialized_value = maybe_serialize( $value );
937 $result = $wpdb->update( $wpdb->sitemeta, array( 'meta_value' => $serialized_value ), array( 'site_id' => $wpdb->siteid, 'meta_key' => $option ) );
940 $cache_key = "{$wpdb->siteid}:$option";
941 wp_cache_set( $cache_key, $value, 'site-options' );
946 do_action( "update_site_option_{$option}", $option, $value, $old_value );
947 do_action( "update_site_option", $option, $value, $old_value );
954 * Delete a site transient.
958 * @subpackage Transient
960 * @uses do_action() Calls 'delete_site_transient_$transient' hook before transient is deleted.
961 * @uses do_action() Calls 'deleted_site_transient' hook on success.
963 * @param string $transient Transient name. Expected to not be SQL-escaped.
964 * @return bool True if successful, false otherwise
966 function delete_site_transient( $transient ) {
967 do_action( 'delete_site_transient_' . $transient, $transient );
968 if ( wp_using_ext_object_cache() ) {
969 $result = wp_cache_delete( $transient, 'site-transient' );
971 $option_timeout = '_site_transient_timeout_' . $transient;
972 $option = '_site_transient_' . $transient;
973 $result = delete_site_option( $option );
975 delete_site_option( $option_timeout );
978 do_action( 'deleted_site_transient', $transient );
983 * Get the value of a site transient.
985 * If the transient does not exist or does not have a value, then the return value
988 * @see get_transient()
991 * @subpackage Transient
993 * @uses apply_filters() Calls 'pre_site_transient_$transient' hook before checking the transient.
994 * Any value other than false will "short-circuit" the retrieval of the transient
995 * and return the returned value.
996 * @uses apply_filters() Calls 'site_transient_$option' hook, after checking the transient, with
997 * the transient value.
999 * @param string $transient Transient name. Expected to not be SQL-escaped.
1000 * @return mixed Value of transient
1002 function get_site_transient( $transient ) {
1003 $pre = apply_filters( 'pre_site_transient_' . $transient, false );
1004 if ( false !== $pre )
1007 if ( wp_using_ext_object_cache() ) {
1008 $value = wp_cache_get( $transient, 'site-transient' );
1010 // Core transients that do not have a timeout. Listed here so querying timeouts can be avoided.
1011 $no_timeout = array('update_core', 'update_plugins', 'update_themes');
1012 $transient_option = '_site_transient_' . $transient;
1013 if ( ! in_array( $transient, $no_timeout ) ) {
1014 $transient_timeout = '_site_transient_timeout_' . $transient;
1015 $timeout = get_site_option( $transient_timeout );
1016 if ( false !== $timeout && $timeout < time() ) {
1017 delete_site_option( $transient_option );
1018 delete_site_option( $transient_timeout );
1023 if ( ! isset( $value ) )
1024 $value = get_site_option( $transient_option );
1027 return apply_filters( 'site_transient_' . $transient, $value );
1031 * Set/update the value of a site transient.
1033 * You do not need to serialize values, if the value needs to be serialize, then
1034 * it will be serialized before it is set.
1036 * @see set_transient()
1038 * @package WordPress
1039 * @subpackage Transient
1041 * @uses apply_filters() Calls 'pre_set_site_transient_$transient' hook to allow overwriting the
1042 * transient value to be stored.
1043 * @uses do_action() Calls 'set_site_transient_$transient' and 'setted_site_transient' hooks on success.
1045 * @param string $transient Transient name. Expected to not be SQL-escaped.
1046 * @param mixed $value Transient value. Expected to not be SQL-escaped.
1047 * @param int $expiration Time until expiration in seconds, default 0
1048 * @return bool False if value was not set and true if value was set.
1050 function set_site_transient( $transient, $value, $expiration = 0 ) {
1051 $value = apply_filters( 'pre_set_site_transient_' . $transient, $value );
1052 $expiration = (int) $expiration;
1054 if ( wp_using_ext_object_cache() ) {
1055 $result = wp_cache_set( $transient, $value, 'site-transient', $expiration );
1057 $transient_timeout = '_site_transient_timeout_' . $transient;
1058 $option = '_site_transient_' . $transient;
1059 if ( false === get_site_option( $option ) ) {
1061 add_site_option( $transient_timeout, time() + $expiration );
1062 $result = add_site_option( $option, $value );
1065 update_site_option( $transient_timeout, time() + $expiration );
1066 $result = update_site_option( $option, $value );
1070 do_action( 'set_site_transient_' . $transient, $value, $expiration );
1071 do_action( 'setted_site_transient', $transient, $value, $expiration );