10 * Retrieve option value based on name of option.
12 * If the option does not exist or does not have a value, then the return value
13 * will be false. This is useful to check whether you need to install an option
14 * and is commonly used during installation of plugin options and to test
15 * whether upgrading is required.
17 * If the option was serialized then it will be unserialized when it is returned.
21 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
22 * @param mixed $default Optional. Default value to return if the option does not exist.
23 * @return mixed Value set for the option.
25 function get_option( $option, $default = false ) {
28 $option = trim( $option );
29 if ( empty( $option ) )
33 * Filter the value of an existing option before it is retrieved.
35 * The dynamic portion of the hook name, `$option`, refers to the option name.
37 * Passing a truthy value to the filter will short-circuit retrieving
38 * the option value, returning the passed value instead.
42 * @param bool|mixed $pre_option Value to return instead of the option value.
43 * Default false to skip it.
45 $pre = apply_filters( 'pre_option_' . $option, false );
49 if ( defined( 'WP_SETUP_CONFIG' ) )
52 if ( ! defined( 'WP_INSTALLING' ) ) {
53 // prevent non-existent options from triggering multiple queries
54 $notoptions = wp_cache_get( 'notoptions', 'options' );
55 if ( isset( $notoptions[ $option ] ) ) {
57 * Filter the default value for an option.
59 * The dynamic portion of the hook name, `$option`, refers to the option name.
63 * @param mixed $default The default value to return if the option does not exist
66 return apply_filters( 'default_option_' . $option, $default );
69 $alloptions = wp_load_alloptions();
71 if ( isset( $alloptions[$option] ) ) {
72 $value = $alloptions[$option];
74 $value = wp_cache_get( $option, 'options' );
76 if ( false === $value ) {
77 $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
79 // Has to be get_row instead of get_var because of funkiness with 0, false, null values
80 if ( is_object( $row ) ) {
81 $value = $row->option_value;
82 wp_cache_add( $option, $value, 'options' );
83 } else { // option does not exist, so we must cache its non-existence
84 $notoptions[$option] = true;
85 wp_cache_set( 'notoptions', $notoptions, 'options' );
87 /** This filter is documented in wp-includes/option.php */
88 return apply_filters( 'default_option_' . $option, $default );
93 $suppress = $wpdb->suppress_errors();
94 $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
95 $wpdb->suppress_errors( $suppress );
96 if ( is_object( $row ) ) {
97 $value = $row->option_value;
99 /** This filter is documented in wp-includes/option.php */
100 return apply_filters( 'default_option_' . $option, $default );
104 // If home is not set use siteurl.
105 if ( 'home' == $option && '' == $value )
106 return get_option( 'siteurl' );
108 if ( in_array( $option, array('siteurl', 'home', 'category_base', 'tag_base') ) )
109 $value = untrailingslashit( $value );
112 * Filter the value of an existing option.
114 * The dynamic portion of the hook name, `$option`, refers to the option name.
116 * @since 1.5.0 As 'option_' . $setting
119 * @param mixed $value Value of the option. If stored serialized, it will be
120 * unserialized prior to being returned.
122 return apply_filters( 'option_' . $option, maybe_unserialize( $value ) );
126 * Protect WordPress special option from being modified.
128 * Will die if $option is in protected list. Protected options are 'alloptions'
129 * and 'notoptions' options.
133 * @param string $option Option name.
135 function wp_protect_special_option( $option ) {
136 if ( 'alloptions' === $option || 'notoptions' === $option )
137 wp_die( sprintf( __( '%s is a protected WP option and may not be modified' ), esc_html( $option ) ) );
141 * Print option value after sanitizing for forms.
145 * @param string $option Option name.
147 function form_option( $option ) {
148 echo esc_attr( get_option( $option ) );
152 * Loads and caches all autoloaded options, if available or all options.
156 * @return array List of all options.
158 function wp_load_alloptions() {
161 if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
162 $alloptions = wp_cache_get( 'alloptions', 'options' );
166 if ( !$alloptions ) {
167 $suppress = $wpdb->suppress_errors();
168 if ( !$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) )
169 $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
170 $wpdb->suppress_errors($suppress);
171 $alloptions = array();
172 foreach ( (array) $alloptions_db as $o ) {
173 $alloptions[$o->option_name] = $o->option_value;
175 if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
176 wp_cache_add( 'alloptions', $alloptions, 'options' );
183 * Loads and caches certain often requested site options if is_multisite() and a persistent cache is not being used.
187 * @param int $site_id Optional site ID for which to query the options. Defaults to the current site.
189 function wp_load_core_site_options( $site_id = null ) {
192 if ( !is_multisite() || wp_using_ext_object_cache() || defined( 'WP_INSTALLING' ) )
195 if ( empty($site_id) )
196 $site_id = $wpdb->siteid;
198 $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' );
200 $core_options_in = "'" . implode("', '", $core_options) . "'";
201 $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) );
203 foreach ( $options as $option ) {
204 $key = $option->meta_key;
205 $cache_key = "{$site_id}:$key";
206 $option->meta_value = maybe_unserialize( $option->meta_value );
208 wp_cache_set( $cache_key, $option->meta_value, 'site-options' );
213 * Update the value of an option that was already added.
215 * You do not need to serialize values. If the value needs to be serialized, then
216 * it will be serialized before it is inserted into the database. Remember,
217 * resources can not be serialized or added as an option.
219 * If the option does not exist, then the option will be added with the option
220 * value, but you will not be able to set whether it is autoloaded. If you want
221 * to set whether an option is autoloaded, then you need to use the add_option().
224 * @since 4.2.0 The `$autoload` parameter was added.
226 * @param string $option Option name. Expected to not be SQL-escaped.
227 * @param mixed $value Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
228 * @param string|bool $autoload Optional. Whether to load the option when WordPress starts up. For existing options,
229 * `$autoload` can only be updated using `update_option()` if `$value` is also changed.
230 * Accepts 'yes' or true to enable, 'no' or false to disable. For non-existent options,
231 * the default value is 'yes'.
232 * @return bool False if value was not updated and true if value was updated.
234 function update_option( $option, $value, $autoload = null ) {
237 $option = trim($option);
238 if ( empty($option) )
241 wp_protect_special_option( $option );
243 if ( is_object( $value ) )
244 $value = clone $value;
246 $value = sanitize_option( $option, $value );
247 $old_value = get_option( $option );
250 * Filter a specific option before its value is (maybe) serialized and updated.
252 * The dynamic portion of the hook name, `$option`, refers to the option name.
256 * @param mixed $value The new, unserialized option value.
257 * @param mixed $old_value The old option value.
259 $value = apply_filters( 'pre_update_option_' . $option, $value, $old_value );
262 * Filter an option before its value is (maybe) serialized and updated.
266 * @param mixed $value The new, unserialized option value.
267 * @param string $option Name of the option.
268 * @param mixed $old_value The old option value.
270 $value = apply_filters( 'pre_update_option', $value, $option, $old_value );
272 // If the new and old values are the same, no need to update.
273 if ( $value === $old_value )
276 /** This filter is documented in wp-includes/option.php */
277 if ( apply_filters( 'default_option_' . $option, false ) === $old_value ) {
278 // Default setting for new options is 'yes'.
279 if ( null === $autoload ) {
283 return add_option( $option, $value, '', $autoload );
286 $serialized_value = maybe_serialize( $value );
289 * Fires immediately before an option value is updated.
293 * @param string $option Name of the option to update.
294 * @param mixed $old_value The old option value.
295 * @param mixed $value The new option value.
297 do_action( 'update_option', $option, $old_value, $value );
299 $update_args = array(
300 'option_value' => $serialized_value,
303 if ( null !== $autoload ) {
304 $update_args['autoload'] = ( 'no' === $autoload || false === $autoload ) ? 'no' : 'yes';
307 $result = $wpdb->update( $wpdb->options, $update_args, array( 'option_name' => $option ) );
311 $notoptions = wp_cache_get( 'notoptions', 'options' );
312 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
313 unset( $notoptions[$option] );
314 wp_cache_set( 'notoptions', $notoptions, 'options' );
317 if ( ! defined( 'WP_INSTALLING' ) ) {
318 $alloptions = wp_load_alloptions();
319 if ( isset( $alloptions[$option] ) ) {
320 $alloptions[ $option ] = $serialized_value;
321 wp_cache_set( 'alloptions', $alloptions, 'options' );
323 wp_cache_set( $option, $serialized_value, 'options' );
328 * Fires after the value of a specific option has been successfully updated.
330 * The dynamic portion of the hook name, `$option`, refers to the option name.
334 * @param mixed $old_value The old option value.
335 * @param mixed $value The new option value.
337 do_action( "update_option_{$option}", $old_value, $value );
340 * Fires after the value of an option has been successfully updated.
344 * @param string $option Name of the updated option.
345 * @param mixed $old_value The old option value.
346 * @param mixed $value The new option value.
348 do_action( 'updated_option', $option, $old_value, $value );
355 * You do not need to serialize values. If the value needs to be serialized, then
356 * it will be serialized before it is inserted into the database. Remember,
357 * resources can not be serialized or added as an option.
359 * You can create options without values and then update the values later.
360 * Existing options will not be updated and checks are performed to ensure that you
361 * aren't adding a protected WordPress option. Care should be taken to not name
362 * options the same as the ones which are protected.
366 * @param string $option Name of option to add. Expected to not be SQL-escaped.
367 * @param mixed $value Optional. Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
368 * @param string $deprecated Optional. Description. Not used anymore.
369 * @param string|bool $autoload Optional. Whether to load the option when WordPress starts up.
370 * Default is enabled. Accepts 'no' to disable for legacy reasons.
371 * @return bool False if option was not added and true if option was added.
373 function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) {
376 if ( !empty( $deprecated ) )
377 _deprecated_argument( __FUNCTION__, '2.3' );
379 $option = trim($option);
380 if ( empty($option) )
383 wp_protect_special_option( $option );
385 if ( is_object($value) )
386 $value = clone $value;
388 $value = sanitize_option( $option, $value );
390 // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
391 $notoptions = wp_cache_get( 'notoptions', 'options' );
392 if ( !is_array( $notoptions ) || !isset( $notoptions[$option] ) )
393 /** This filter is documented in wp-includes/option.php */
394 if ( apply_filters( 'default_option_' . $option, false ) !== get_option( $option ) )
397 $serialized_value = maybe_serialize( $value );
398 $autoload = ( 'no' === $autoload || false === $autoload ) ? 'no' : 'yes';
401 * Fires before an option is added.
405 * @param string $option Name of the option to add.
406 * @param mixed $value Value of the option.
408 do_action( 'add_option', $option, $value );
410 $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 ) );
414 if ( ! defined( 'WP_INSTALLING' ) ) {
415 if ( 'yes' == $autoload ) {
416 $alloptions = wp_load_alloptions();
417 $alloptions[ $option ] = $serialized_value;
418 wp_cache_set( 'alloptions', $alloptions, 'options' );
420 wp_cache_set( $option, $serialized_value, 'options' );
424 // This option exists now
425 $notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh
426 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
427 unset( $notoptions[$option] );
428 wp_cache_set( 'notoptions', $notoptions, 'options' );
432 * Fires after a specific option has been added.
434 * The dynamic portion of the hook name, `$option`, refers to the option name.
436 * @since 2.5.0 As "add_option_{$name}"
439 * @param string $option Name of the option to add.
440 * @param mixed $value Value of the option.
442 do_action( "add_option_{$option}", $option, $value );
445 * Fires after an option has been added.
449 * @param string $option Name of the added option.
450 * @param mixed $value Value of the option.
452 do_action( 'added_option', $option, $value );
457 * Removes option by name. Prevents removal of protected WordPress options.
461 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
462 * @return bool True, if option is successfully deleted. False on failure.
464 function delete_option( $option ) {
467 $option = trim( $option );
468 if ( empty( $option ) )
471 wp_protect_special_option( $option );
473 // Get the ID, if no ID then return
474 $row = $wpdb->get_row( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) );
475 if ( is_null( $row ) )
479 * Fires immediately before an option is deleted.
483 * @param string $option Name of the option to delete.
485 do_action( 'delete_option', $option );
487 $result = $wpdb->delete( $wpdb->options, array( 'option_name' => $option ) );
488 if ( ! defined( 'WP_INSTALLING' ) ) {
489 if ( 'yes' == $row->autoload ) {
490 $alloptions = wp_load_alloptions();
491 if ( is_array( $alloptions ) && isset( $alloptions[$option] ) ) {
492 unset( $alloptions[$option] );
493 wp_cache_set( 'alloptions', $alloptions, 'options' );
496 wp_cache_delete( $option, 'options' );
502 * Fires after a specific option has been deleted.
504 * The dynamic portion of the hook name, `$option`, refers to the option name.
508 * @param string $option Name of the deleted option.
510 do_action( "delete_option_$option", $option );
513 * Fires after an option has been deleted.
517 * @param string $option Name of the deleted option.
519 do_action( 'deleted_option', $option );
526 * Delete a transient.
530 * @param string $transient Transient name. Expected to not be SQL-escaped.
531 * @return bool true if successful, false otherwise
533 function delete_transient( $transient ) {
536 * Fires immediately before a specific transient is deleted.
538 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
542 * @param string $transient Transient name.
544 do_action( 'delete_transient_' . $transient, $transient );
546 if ( wp_using_ext_object_cache() ) {
547 $result = wp_cache_delete( $transient, 'transient' );
549 $option_timeout = '_transient_timeout_' . $transient;
550 $option = '_transient_' . $transient;
551 $result = delete_option( $option );
553 delete_option( $option_timeout );
559 * Fires after a transient is deleted.
563 * @param string $transient Deleted transient name.
565 do_action( 'deleted_transient', $transient );
572 * Get the value of a transient.
574 * If the transient does not exist, does not have a value, or has expired,
575 * then the return value will be false.
579 * @param string $transient Transient name. Expected to not be SQL-escaped.
580 * @return mixed Value of transient.
582 function get_transient( $transient ) {
585 * Filter the value of an existing transient.
587 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
589 * Passing a truthy value to the filter will effectively short-circuit retrieval
590 * of the transient, returning the passed value instead.
594 * @param mixed $pre_transient The default value to return if the transient does not exist.
595 * Any value other than false will short-circuit the retrieval
596 * of the transient, and return the returned value.
598 $pre = apply_filters( 'pre_transient_' . $transient, false );
599 if ( false !== $pre )
602 if ( wp_using_ext_object_cache() ) {
603 $value = wp_cache_get( $transient, 'transient' );
605 $transient_option = '_transient_' . $transient;
606 if ( ! defined( 'WP_INSTALLING' ) ) {
607 // If option is not in alloptions, it is not autoloaded and thus has a timeout
608 $alloptions = wp_load_alloptions();
609 if ( !isset( $alloptions[$transient_option] ) ) {
610 $transient_timeout = '_transient_timeout_' . $transient;
611 if ( get_option( $transient_timeout ) < time() ) {
612 delete_option( $transient_option );
613 delete_option( $transient_timeout );
619 if ( ! isset( $value ) )
620 $value = get_option( $transient_option );
624 * Filter an existing transient's value.
626 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
630 * @param mixed $value Value of transient.
632 return apply_filters( 'transient_' . $transient, $value );
636 * Set/update the value of a transient.
638 * You do not need to serialize values. If the value needs to be serialized, then
639 * it will be serialized before it is set.
643 * @param string $transient Transient name. Expected to not be SQL-escaped. Must be
644 * 45 characters or fewer in length.
645 * @param mixed $value Transient value. Must be serializable if non-scalar.
646 * Expected to not be SQL-escaped.
647 * @param int $expiration Optional. Time until expiration in seconds. Default 0.
648 * @return bool False if value was not set and true if value was set.
650 function set_transient( $transient, $value, $expiration = 0 ) {
652 $expiration = (int) $expiration;
655 * Filter a specific transient before its value is set.
657 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
660 * @since 4.2.0 Added `$expiration` parameter.
662 * @param mixed $value New value of transient.
663 * @param int $expiration Time until expiration in seconds.
665 $value = apply_filters( 'pre_set_transient_' . $transient, $value, $expiration );
667 if ( wp_using_ext_object_cache() ) {
668 $result = wp_cache_set( $transient, $value, 'transient', $expiration );
670 $transient_timeout = '_transient_timeout_' . $transient;
671 $transient = '_transient_' . $transient;
672 if ( false === get_option( $transient ) ) {
676 add_option( $transient_timeout, time() + $expiration, '', 'no' );
678 $result = add_option( $transient, $value, '', $autoload );
680 // If expiration is requested, but the transient has no timeout option,
681 // delete, then re-create transient rather than update.
684 if ( false === get_option( $transient_timeout ) ) {
685 delete_option( $transient );
686 add_option( $transient_timeout, time() + $expiration, '', 'no' );
687 $result = add_option( $transient, $value, '', 'no' );
690 update_option( $transient_timeout, time() + $expiration );
694 $result = update_option( $transient, $value );
702 * Fires after the value for a specific transient has been set.
704 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
708 * @param mixed $value Transient value.
709 * @param int $expiration Time until expiration in seconds. Default 0.
711 do_action( 'set_transient_' . $transient, $value, $expiration );
714 * Fires after the value for a transient has been set.
718 * @param string $transient The name of the transient.
719 * @param mixed $value Transient value.
720 * @param int $expiration Time until expiration in seconds. Default 0.
722 do_action( 'setted_transient', $transient, $value, $expiration );
728 * Saves and restores user interface settings stored in a cookie.
730 * Checks if the current user-settings cookie is updated and stores it. When no
731 * cookie exists (different browser used), adds the last saved cookie restoring
736 function wp_user_settings() {
738 if ( ! is_admin() || defined( 'DOING_AJAX' ) ) {
742 if ( ! $user_id = get_current_user_id() ) {
746 if ( is_super_admin() && ! is_user_member_of_blog() ) {
750 $settings = (string) get_user_option( 'user-settings', $user_id );
752 if ( isset( $_COOKIE['wp-settings-' . $user_id] ) ) {
753 $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user_id] );
755 // No change or both empty
756 if ( $cookie == $settings )
759 $last_saved = (int) get_user_option( 'user-settings-time', $user_id );
760 $current = isset( $_COOKIE['wp-settings-time-' . $user_id]) ? preg_replace( '/[^0-9]/', '', $_COOKIE['wp-settings-time-' . $user_id] ) : 0;
762 // The cookie is newer than the saved value. Update the user_option and leave the cookie as-is
763 if ( $current > $last_saved ) {
764 update_user_option( $user_id, 'user-settings', $cookie, false );
765 update_user_option( $user_id, 'user-settings-time', time() - 5, false );
770 // The cookie is not set in the current browser or the saved value is newer.
771 $secure = ( 'https' === parse_url( site_url(), PHP_URL_SCHEME ) );
772 setcookie( 'wp-settings-' . $user_id, $settings, time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure );
773 setcookie( 'wp-settings-time-' . $user_id, time(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure );
774 $_COOKIE['wp-settings-' . $user_id] = $settings;
778 * Retrieve user interface setting value based on setting name.
782 * @param string $name The name of the setting.
783 * @param string $default Optional default value to return when $name is not set.
784 * @return mixed the last saved user setting or the default value/false if it doesn't exist.
786 function get_user_setting( $name, $default = false ) {
787 $all_user_settings = get_all_user_settings();
789 return isset( $all_user_settings[$name] ) ? $all_user_settings[$name] : $default;
793 * Add or update user interface setting.
795 * Both $name and $value can contain only ASCII letters, numbers and underscores.
796 * This function has to be used before any output has started as it calls setcookie().
800 * @param string $name The name of the setting.
801 * @param string $value The value for the setting.
802 * @return null|bool true if set successfully/false if not.
804 function set_user_setting( $name, $value ) {
806 if ( headers_sent() ) {
810 $all_user_settings = get_all_user_settings();
811 $all_user_settings[$name] = $value;
813 return wp_set_all_user_settings( $all_user_settings );
817 * Delete user interface settings.
819 * Deleting settings would reset them to the defaults.
820 * This function has to be used before any output has started as it calls setcookie().
824 * @param string $names The name or array of names of the setting to be deleted.
825 * @return null|bool true if deleted successfully/false if not.
827 function delete_user_setting( $names ) {
829 if ( headers_sent() ) {
833 $all_user_settings = get_all_user_settings();
834 $names = (array) $names;
837 foreach ( $names as $name ) {
838 if ( isset( $all_user_settings[$name] ) ) {
839 unset( $all_user_settings[$name] );
845 return wp_set_all_user_settings( $all_user_settings );
852 * Retrieve all user interface settings.
856 * @return array the last saved user settings or empty array.
858 function get_all_user_settings() {
859 global $_updated_user_settings;
861 if ( ! $user_id = get_current_user_id() ) {
865 if ( isset( $_updated_user_settings ) && is_array( $_updated_user_settings ) ) {
866 return $_updated_user_settings;
869 $user_settings = array();
871 if ( isset( $_COOKIE['wp-settings-' . $user_id] ) ) {
872 $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user_id] );
874 if ( strpos( $cookie, '=' ) ) { // '=' cannot be 1st char
875 parse_str( $cookie, $user_settings );
878 $option = get_user_option( 'user-settings', $user_id );
880 if ( $option && is_string( $option ) ) {
881 parse_str( $option, $user_settings );
885 $_updated_user_settings = $user_settings;
886 return $user_settings;
890 * Private. Set all user interface settings.
894 * @param array $user_settings
897 function wp_set_all_user_settings( $user_settings ) {
898 global $_updated_user_settings;
900 if ( ! $user_id = get_current_user_id() ) {
904 if ( is_super_admin() && ! is_user_member_of_blog() ) {
909 foreach ( $user_settings as $name => $value ) {
910 $_name = preg_replace( '/[^A-Za-z0-9_]+/', '', $name );
911 $_value = preg_replace( '/[^A-Za-z0-9_]+/', '', $value );
913 if ( ! empty( $_name ) ) {
914 $settings .= $_name . '=' . $_value . '&';
918 $settings = rtrim( $settings, '&' );
919 parse_str( $settings, $_updated_user_settings );
921 update_user_option( $user_id, 'user-settings', $settings, false );
922 update_user_option( $user_id, 'user-settings-time', time(), false );
928 * Delete the user settings of the current user.
932 function delete_all_user_settings() {
933 if ( ! $user_id = get_current_user_id() ) {
937 update_user_option( $user_id, 'user-settings', '', false );
938 setcookie( 'wp-settings-' . $user_id, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH );
942 * Retrieve site option value based on name of option.
948 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
949 * @param mixed $default Optional value to return if option doesn't exist. Default false.
950 * @param bool $use_cache Whether to use cache. Multisite only. Default true.
951 * @return mixed Value set for the option.
953 function get_site_option( $option, $default = false, $use_cache = true ) {
957 * Filter an existing site option before it is retrieved.
959 * The dynamic portion of the hook name, `$option`, refers to the option name.
961 * Passing a truthy value to the filter will effectively short-circuit retrieval,
962 * returning the passed value instead.
964 * @since 2.9.0 As 'pre_site_option_' . $key
967 * @param mixed $pre_option The default value to return if the option does not exist.
969 $pre = apply_filters( 'pre_site_option_' . $option, false );
971 if ( false !== $pre )
974 // prevent non-existent options from triggering multiple queries
975 $notoptions_key = "{$wpdb->siteid}:notoptions";
976 $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
978 if ( isset( $notoptions[$option] ) ) {
981 * Filter a specific default site option.
983 * The dynamic portion of the hook name, `$option`, refers to the option name.
987 * @param mixed $default The value to return if the site option does not exist
990 return apply_filters( 'default_site_option_' . $option, $default );
993 if ( ! is_multisite() ) {
995 /** This filter is documented in wp-includes/option.php */
996 $default = apply_filters( 'default_site_option_' . $option, $default );
997 $value = get_option($option, $default);
999 $cache_key = "{$wpdb->siteid}:$option";
1001 $value = wp_cache_get($cache_key, 'site-options');
1003 if ( !isset($value) || (false === $value) ) {
1004 $row = $wpdb->get_row( $wpdb->prepare("SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );
1006 // Has to be get_row instead of get_var because of funkiness with 0, false, null values
1007 if ( is_object( $row ) ) {
1008 $value = $row->meta_value;
1009 $value = maybe_unserialize( $value );
1010 wp_cache_set( $cache_key, $value, 'site-options' );
1012 $notoptions[$option] = true;
1013 wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
1015 /** This filter is documented in wp-includes/option.php */
1016 $value = apply_filters( 'default_site_option_' . $option, $default );
1022 * Filter the value of an existing site option.
1024 * The dynamic portion of the hook name, `$option`, refers to the option name.
1026 * @since 2.9.0 As 'site_option_' . $key
1029 * @param mixed $value Value of site option.
1031 return apply_filters( 'site_option_' . $option, $value );
1035 * Add a new site option.
1037 * Existing options will not be updated. Note that prior to 3.3 this wasn't the case.
1043 * @param string $option Name of option to add. Expected to not be SQL-escaped.
1044 * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
1045 * @return bool False if option was not added and true if option was added.
1047 function add_site_option( $option, $value ) {
1050 wp_protect_special_option( $option );
1053 * Filter the value of a specific site option before it is added.
1055 * The dynamic portion of the hook name, `$option`, refers to the option name.
1057 * @since 2.9.0 As 'pre_add_site_option_' . $key
1060 * @param mixed $value Value of site option.
1062 $value = apply_filters( 'pre_add_site_option_' . $option, $value );
1064 $notoptions_key = "{$wpdb->siteid}:notoptions";
1066 if ( !is_multisite() ) {
1067 $result = add_option( $option, $value );
1069 $cache_key = "{$wpdb->siteid}:$option";
1071 // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
1072 $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
1073 if ( ! is_array( $notoptions ) || ! isset( $notoptions[$option] ) )
1074 if ( false !== get_site_option( $option ) )
1077 $value = sanitize_option( $option, $value );
1079 $serialized_value = maybe_serialize( $value );
1080 $result = $wpdb->insert( $wpdb->sitemeta, array('site_id' => $wpdb->siteid, 'meta_key' => $option, 'meta_value' => $serialized_value ) );
1085 wp_cache_set( $cache_key, $value, 'site-options' );
1087 // This option exists now
1088 $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); // yes, again... we need it to be fresh
1089 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
1090 unset( $notoptions[$option] );
1091 wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
1098 * Fires after a specific site option has been successfully added.
1100 * The dynamic portion of the hook name, `$option`, refers to the option name.
1102 * @since 2.9.0 As "add_site_option_{$key}"
1105 * @param string $option Name of site option.
1106 * @param mixed $value Value of site option.
1108 do_action( "add_site_option_{$option}", $option, $value );
1111 * Fires after a site option has been successfully added.
1115 * @param string $option Name of site option.
1116 * @param mixed $value Value of site option.
1118 do_action( "add_site_option", $option, $value );
1126 * Removes site option by name.
1130 * @see delete_option()
1132 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
1133 * @return bool True, if succeed. False, if failure.
1135 function delete_site_option( $option ) {
1138 // ms_protect_special_option( $option ); @todo
1141 * Fires immediately before a specific site option is deleted.
1143 * The dynamic portion of the hook name, `$option`, refers to the option name.
1147 do_action( 'pre_delete_site_option_' . $option );
1149 if ( !is_multisite() ) {
1150 $result = delete_option( $option );
1152 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );
1153 if ( is_null( $row ) || !$row->meta_id )
1155 $cache_key = "{$wpdb->siteid}:$option";
1156 wp_cache_delete( $cache_key, 'site-options' );
1158 $result = $wpdb->delete( $wpdb->sitemeta, array( 'meta_key' => $option, 'site_id' => $wpdb->siteid ) );
1164 * Fires after a specific site option has been deleted.
1166 * The dynamic portion of the hook name, `$option`, refers to the option name.
1168 * @since 2.9.0 As "delete_site_option_{$key}"
1171 * @param string $option Name of the site option.
1173 do_action( "delete_site_option_{$option}", $option );
1176 * Fires after a site option has been deleted.
1180 * @param string $option Name of the site option.
1182 do_action( "delete_site_option", $option );
1190 * Update the value of a site option that was already added.
1194 * @see update_option()
1196 * @param string $option Name of option. Expected to not be SQL-escaped.
1197 * @param mixed $value Option value. Expected to not be SQL-escaped.
1198 * @return bool False if value was not updated and true if value was updated.
1200 function update_site_option( $option, $value ) {
1203 wp_protect_special_option( $option );
1205 $old_value = get_site_option( $option );
1208 * Filter a specific site option before its value is updated.
1210 * The dynamic portion of the hook name, `$option`, refers to the option name.
1212 * @since 2.9.0 As 'pre_update_site_option_' . $key
1215 * @param mixed $value New value of site option.
1216 * @param mixed $old_value Old value of site option.
1218 $value = apply_filters( 'pre_update_site_option_' . $option, $value, $old_value );
1220 if ( $value === $old_value )
1223 if ( false === $old_value )
1224 return add_site_option( $option, $value );
1226 $notoptions_key = "{$wpdb->siteid}:notoptions";
1227 $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
1228 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
1229 unset( $notoptions[$option] );
1230 wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
1233 if ( !is_multisite() ) {
1234 $result = update_option( $option, $value );
1236 $value = sanitize_option( $option, $value );
1238 $serialized_value = maybe_serialize( $value );
1239 $result = $wpdb->update( $wpdb->sitemeta, array( 'meta_value' => $serialized_value ), array( 'site_id' => $wpdb->siteid, 'meta_key' => $option ) );
1242 $cache_key = "{$wpdb->siteid}:$option";
1243 wp_cache_set( $cache_key, $value, 'site-options' );
1250 * Fires after the value of a specific site option has been successfully updated.
1252 * The dynamic portion of the hook name, `$option`, refers to the option name.
1254 * @since 2.9.0 As "update_site_option_{$key}"
1257 * @param string $option Name of site option.
1258 * @param mixed $value Current value of site option.
1259 * @param mixed $old_value Old value of site option.
1261 do_action( "update_site_option_{$option}", $option, $value, $old_value );
1264 * Fires after the value of a site option has been successfully updated.
1268 * @param string $option Name of site option.
1269 * @param mixed $value Current value of site option.
1270 * @param mixed $old_value Old value of site option.
1272 do_action( "update_site_option", $option, $value, $old_value );
1280 * Delete a site transient.
1284 * @param string $transient Transient name. Expected to not be SQL-escaped.
1285 * @return bool True if successful, false otherwise
1287 function delete_site_transient( $transient ) {
1290 * Fires immediately before a specific site transient is deleted.
1292 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1296 * @param string $transient Transient name.
1298 do_action( 'delete_site_transient_' . $transient, $transient );
1300 if ( wp_using_ext_object_cache() ) {
1301 $result = wp_cache_delete( $transient, 'site-transient' );
1303 $option_timeout = '_site_transient_timeout_' . $transient;
1304 $option = '_site_transient_' . $transient;
1305 $result = delete_site_option( $option );
1307 delete_site_option( $option_timeout );
1312 * Fires after a transient is deleted.
1316 * @param string $transient Deleted transient name.
1318 do_action( 'deleted_site_transient', $transient );
1325 * Get the value of a site transient.
1327 * If the transient does not exist, does not have a value, or has expired,
1328 * then the return value will be false.
1332 * @see get_transient()
1334 * @param string $transient Transient name. Expected to not be SQL-escaped.
1335 * @return mixed Value of transient.
1337 function get_site_transient( $transient ) {
1340 * Filter the value of an existing site transient.
1342 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1344 * Passing a truthy value to the filter will effectively short-circuit retrieval,
1345 * returning the passed value instead.
1349 * @param mixed $pre_site_transient The default value to return if the site transient does not exist.
1350 * Any value other than false will short-circuit the retrieval
1351 * of the transient, and return the returned value.
1353 $pre = apply_filters( 'pre_site_transient_' . $transient, false );
1355 if ( false !== $pre )
1358 if ( wp_using_ext_object_cache() ) {
1359 $value = wp_cache_get( $transient, 'site-transient' );
1361 // Core transients that do not have a timeout. Listed here so querying timeouts can be avoided.
1362 $no_timeout = array('update_core', 'update_plugins', 'update_themes');
1363 $transient_option = '_site_transient_' . $transient;
1364 if ( ! in_array( $transient, $no_timeout ) ) {
1365 $transient_timeout = '_site_transient_timeout_' . $transient;
1366 $timeout = get_site_option( $transient_timeout );
1367 if ( false !== $timeout && $timeout < time() ) {
1368 delete_site_option( $transient_option );
1369 delete_site_option( $transient_timeout );
1374 if ( ! isset( $value ) )
1375 $value = get_site_option( $transient_option );
1379 * Filter the value of an existing site transient.
1381 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1385 * @param mixed $value Value of site transient.
1387 return apply_filters( 'site_transient_' . $transient, $value );
1391 * Set/update the value of a site transient.
1393 * You do not need to serialize values, if the value needs to be serialize, then
1394 * it will be serialized before it is set.
1398 * @see set_transient()
1400 * @param string $transient Transient name. Expected to not be SQL-escaped. Must be
1401 * 40 characters or fewer in length.
1402 * @param mixed $value Transient value. Expected to not be SQL-escaped.
1403 * @param int $expiration Optional. Time until expiration in seconds. Default 0.
1404 * @return bool False if value was not set and true if value was set.
1406 function set_site_transient( $transient, $value, $expiration = 0 ) {
1409 * Filter the value of a specific site transient before it is set.
1411 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1415 * @param mixed $value Value of site transient.
1417 $value = apply_filters( 'pre_set_site_transient_' . $transient, $value );
1419 $expiration = (int) $expiration;
1421 if ( wp_using_ext_object_cache() ) {
1422 $result = wp_cache_set( $transient, $value, 'site-transient', $expiration );
1424 $transient_timeout = '_site_transient_timeout_' . $transient;
1425 $option = '_site_transient_' . $transient;
1426 if ( false === get_site_option( $option ) ) {
1428 add_site_option( $transient_timeout, time() + $expiration );
1429 $result = add_site_option( $option, $value );
1432 update_site_option( $transient_timeout, time() + $expiration );
1433 $result = update_site_option( $option, $value );
1439 * Fires after the value for a specific site transient has been set.
1441 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1445 * @param mixed $value Site transient value.
1446 * @param int $expiration Time until expiration in seconds. Default 0.
1448 do_action( 'set_site_transient_' . $transient, $value, $expiration );
1451 * Fires after the value for a site transient has been set.
1455 * @param string $transient The name of the site transient.
1456 * @param mixed $value Site transient value.
1457 * @param int $expiration Time until expiration in seconds. Default 0.
1459 do_action( 'setted_site_transient', $transient, $value, $expiration );