10 * Retrieves an option value based on an option name.
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.
19 * Any scalar values will be returned as strings. You may coerce the return type of
20 * a given option by registering an {@see 'option_$option'} filter callback.
24 * @global wpdb $wpdb WordPress database abstraction object.
26 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
27 * @param mixed $default Optional. Default value to return if the option does not exist.
28 * @return mixed Value set for the option.
30 function get_option( $option, $default = false ) {
33 $option = trim( $option );
34 if ( empty( $option ) )
38 * Filters the value of an existing option before it is retrieved.
40 * The dynamic portion of the hook name, `$option`, refers to the option name.
42 * Passing a truthy value to the filter will short-circuit retrieving
43 * the option value, returning the passed value instead.
46 * @since 4.4.0 The `$option` parameter was added.
48 * @param bool|mixed $pre_option Value to return instead of the option value.
49 * Default false to skip it.
50 * @param string $option Option name.
52 $pre = apply_filters( 'pre_option_' . $option, false, $option );
56 if ( defined( 'WP_SETUP_CONFIG' ) )
59 if ( ! wp_installing() ) {
60 // prevent non-existent options from triggering multiple queries
61 $notoptions = wp_cache_get( 'notoptions', 'options' );
62 if ( isset( $notoptions[ $option ] ) ) {
64 * Filters the default value for an option.
66 * The dynamic portion of the hook name, `$option`, refers to the option name.
69 * @since 4.4.0 The `$option` parameter was added.
71 * @param mixed $default The default value to return if the option does not exist
73 * @param string $option Option name.
75 return apply_filters( 'default_option_' . $option, $default, $option );
78 $alloptions = wp_load_alloptions();
80 if ( isset( $alloptions[$option] ) ) {
81 $value = $alloptions[$option];
83 $value = wp_cache_get( $option, 'options' );
85 if ( false === $value ) {
86 $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
88 // Has to be get_row instead of get_var because of funkiness with 0, false, null values
89 if ( is_object( $row ) ) {
90 $value = $row->option_value;
91 wp_cache_add( $option, $value, 'options' );
92 } else { // option does not exist, so we must cache its non-existence
93 if ( ! is_array( $notoptions ) ) {
94 $notoptions = array();
96 $notoptions[$option] = true;
97 wp_cache_set( 'notoptions', $notoptions, 'options' );
99 /** This filter is documented in wp-includes/option.php */
100 return apply_filters( 'default_option_' . $option, $default, $option );
105 $suppress = $wpdb->suppress_errors();
106 $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
107 $wpdb->suppress_errors( $suppress );
108 if ( is_object( $row ) ) {
109 $value = $row->option_value;
111 /** This filter is documented in wp-includes/option.php */
112 return apply_filters( 'default_option_' . $option, $default, $option );
116 // If home is not set use siteurl.
117 if ( 'home' == $option && '' == $value )
118 return get_option( 'siteurl' );
120 if ( in_array( $option, array('siteurl', 'home', 'category_base', 'tag_base') ) )
121 $value = untrailingslashit( $value );
124 * Filters the value of an existing option.
126 * The dynamic portion of the hook name, `$option`, refers to the option name.
128 * @since 1.5.0 As 'option_' . $setting
130 * @since 4.4.0 The `$option` parameter was added.
132 * @param mixed $value Value of the option. If stored serialized, it will be
133 * unserialized prior to being returned.
134 * @param string $option Option name.
136 return apply_filters( 'option_' . $option, maybe_unserialize( $value ), $option );
140 * Protect WordPress special option from being modified.
142 * Will die if $option is in protected list. Protected options are 'alloptions'
143 * and 'notoptions' options.
147 * @param string $option Option name.
149 function wp_protect_special_option( $option ) {
150 if ( 'alloptions' === $option || 'notoptions' === $option )
151 wp_die( sprintf( __( '%s is a protected WP option and may not be modified' ), esc_html( $option ) ) );
155 * Print option value after sanitizing for forms.
159 * @param string $option Option name.
161 function form_option( $option ) {
162 echo esc_attr( get_option( $option ) );
166 * Loads and caches all autoloaded options, if available or all options.
170 * @global wpdb $wpdb WordPress database abstraction object.
172 * @return array List of all options.
174 function wp_load_alloptions() {
177 if ( ! wp_installing() || ! is_multisite() )
178 $alloptions = wp_cache_get( 'alloptions', 'options' );
182 if ( !$alloptions ) {
183 $suppress = $wpdb->suppress_errors();
184 if ( !$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) )
185 $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
186 $wpdb->suppress_errors($suppress);
187 $alloptions = array();
188 foreach ( (array) $alloptions_db as $o ) {
189 $alloptions[$o->option_name] = $o->option_value;
191 if ( ! wp_installing() || ! is_multisite() )
192 wp_cache_add( 'alloptions', $alloptions, 'options' );
199 * Loads and caches certain often requested site options if is_multisite() and a persistent cache is not being used.
203 * @global wpdb $wpdb WordPress database abstraction object.
205 * @param int $site_id Optional site ID for which to query the options. Defaults to the current site.
207 function wp_load_core_site_options( $site_id = null ) {
210 if ( ! is_multisite() || wp_using_ext_object_cache() || wp_installing() )
213 if ( empty($site_id) )
214 $site_id = $wpdb->siteid;
216 $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' );
218 $core_options_in = "'" . implode("', '", $core_options) . "'";
219 $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) );
221 foreach ( $options as $option ) {
222 $key = $option->meta_key;
223 $cache_key = "{$site_id}:$key";
224 $option->meta_value = maybe_unserialize( $option->meta_value );
226 wp_cache_set( $cache_key, $option->meta_value, 'site-options' );
231 * Update the value of an option that was already added.
233 * You do not need to serialize values. If the value needs to be serialized, then
234 * it will be serialized before it is inserted into the database. Remember,
235 * resources can not be serialized or added as an option.
237 * If the option does not exist, then the option will be added with the option value,
238 * with an `$autoload` value of 'yes'.
241 * @since 4.2.0 The `$autoload` parameter was added.
243 * @global wpdb $wpdb WordPress database abstraction object.
245 * @param string $option Option name. Expected to not be SQL-escaped.
246 * @param mixed $value Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
247 * @param string|bool $autoload Optional. Whether to load the option when WordPress starts up. For existing options,
248 * `$autoload` can only be updated using `update_option()` if `$value` is also changed.
249 * Accepts 'yes'|true to enable or 'no'|false to disable. For non-existent options,
250 * the default value is 'yes'. Default null.
251 * @return bool False if value was not updated and true if value was updated.
253 function update_option( $option, $value, $autoload = null ) {
256 $option = trim($option);
257 if ( empty($option) )
260 wp_protect_special_option( $option );
262 if ( is_object( $value ) )
263 $value = clone $value;
265 $value = sanitize_option( $option, $value );
266 $old_value = get_option( $option );
269 * Filters a specific option before its value is (maybe) serialized and updated.
271 * The dynamic portion of the hook name, `$option`, refers to the option name.
274 * @since 4.4.0 The `$option` parameter was added.
276 * @param mixed $value The new, unserialized option value.
277 * @param mixed $old_value The old option value.
278 * @param string $option Option name.
280 $value = apply_filters( 'pre_update_option_' . $option, $value, $old_value, $option );
283 * Filters an option before its value is (maybe) serialized and updated.
287 * @param mixed $value The new, unserialized option value.
288 * @param string $option Name of the option.
289 * @param mixed $old_value The old option value.
291 $value = apply_filters( 'pre_update_option', $value, $option, $old_value );
293 // If the new and old values are the same, no need to update.
294 if ( $value === $old_value )
297 /** This filter is documented in wp-includes/option.php */
298 if ( apply_filters( 'default_option_' . $option, false, $option ) === $old_value ) {
299 // Default setting for new options is 'yes'.
300 if ( null === $autoload ) {
304 return add_option( $option, $value, '', $autoload );
307 $serialized_value = maybe_serialize( $value );
310 * Fires immediately before an option value is updated.
314 * @param string $option Name of the option to update.
315 * @param mixed $old_value The old option value.
316 * @param mixed $value The new option value.
318 do_action( 'update_option', $option, $old_value, $value );
320 $update_args = array(
321 'option_value' => $serialized_value,
324 if ( null !== $autoload ) {
325 $update_args['autoload'] = ( 'no' === $autoload || false === $autoload ) ? 'no' : 'yes';
328 $result = $wpdb->update( $wpdb->options, $update_args, array( 'option_name' => $option ) );
332 $notoptions = wp_cache_get( 'notoptions', 'options' );
333 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
334 unset( $notoptions[$option] );
335 wp_cache_set( 'notoptions', $notoptions, 'options' );
338 if ( ! wp_installing() ) {
339 $alloptions = wp_load_alloptions();
340 if ( isset( $alloptions[$option] ) ) {
341 $alloptions[ $option ] = $serialized_value;
342 wp_cache_set( 'alloptions', $alloptions, 'options' );
344 wp_cache_set( $option, $serialized_value, 'options' );
349 * Fires after the value of a specific option has been successfully updated.
351 * The dynamic portion of the hook name, `$option`, refers to the option name.
354 * @since 4.4.0 The `$option` parameter was added.
356 * @param mixed $old_value The old option value.
357 * @param mixed $value The new option value.
358 * @param string $option Option name.
360 do_action( "update_option_{$option}", $old_value, $value, $option );
363 * Fires after the value of an option has been successfully updated.
367 * @param string $option Name of the updated option.
368 * @param mixed $old_value The old option value.
369 * @param mixed $value The new option value.
371 do_action( 'updated_option', $option, $old_value, $value );
378 * You do not need to serialize values. If the value needs to be serialized, then
379 * it will be serialized before it is inserted into the database. Remember,
380 * resources can not be serialized or added as an option.
382 * You can create options without values and then update the values later.
383 * Existing options will not be updated and checks are performed to ensure that you
384 * aren't adding a protected WordPress option. Care should be taken to not name
385 * options the same as the ones which are protected.
389 * @global wpdb $wpdb WordPress database abstraction object.
391 * @param string $option Name of option to add. Expected to not be SQL-escaped.
392 * @param mixed $value Optional. Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
393 * @param string $deprecated Optional. Description. Not used anymore.
394 * @param string|bool $autoload Optional. Whether to load the option when WordPress starts up.
395 * Default is enabled. Accepts 'no' to disable for legacy reasons.
396 * @return bool False if option was not added and true if option was added.
398 function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) {
401 if ( !empty( $deprecated ) )
402 _deprecated_argument( __FUNCTION__, '2.3.0' );
404 $option = trim($option);
405 if ( empty($option) )
408 wp_protect_special_option( $option );
410 if ( is_object($value) )
411 $value = clone $value;
413 $value = sanitize_option( $option, $value );
415 // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
416 $notoptions = wp_cache_get( 'notoptions', 'options' );
417 if ( !is_array( $notoptions ) || !isset( $notoptions[$option] ) )
418 /** This filter is documented in wp-includes/option.php */
419 if ( apply_filters( 'default_option_' . $option, false, $option ) !== get_option( $option ) )
422 $serialized_value = maybe_serialize( $value );
423 $autoload = ( 'no' === $autoload || false === $autoload ) ? 'no' : 'yes';
426 * Fires before an option is added.
430 * @param string $option Name of the option to add.
431 * @param mixed $value Value of the option.
433 do_action( 'add_option', $option, $value );
435 $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 ) );
439 if ( ! wp_installing() ) {
440 if ( 'yes' == $autoload ) {
441 $alloptions = wp_load_alloptions();
442 $alloptions[ $option ] = $serialized_value;
443 wp_cache_set( 'alloptions', $alloptions, 'options' );
445 wp_cache_set( $option, $serialized_value, 'options' );
449 // This option exists now
450 $notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh
451 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
452 unset( $notoptions[$option] );
453 wp_cache_set( 'notoptions', $notoptions, 'options' );
457 * Fires after a specific option has been added.
459 * The dynamic portion of the hook name, `$option`, refers to the option name.
461 * @since 2.5.0 As "add_option_{$name}"
464 * @param string $option Name of the option to add.
465 * @param mixed $value Value of the option.
467 do_action( "add_option_{$option}", $option, $value );
470 * Fires after an option has been added.
474 * @param string $option Name of the added option.
475 * @param mixed $value Value of the option.
477 do_action( 'added_option', $option, $value );
482 * Removes option by name. Prevents removal of protected WordPress options.
486 * @global wpdb $wpdb WordPress database abstraction object.
488 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
489 * @return bool True, if option is successfully deleted. False on failure.
491 function delete_option( $option ) {
494 $option = trim( $option );
495 if ( empty( $option ) )
498 wp_protect_special_option( $option );
500 // Get the ID, if no ID then return
501 $row = $wpdb->get_row( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) );
502 if ( is_null( $row ) )
506 * Fires immediately before an option is deleted.
510 * @param string $option Name of the option to delete.
512 do_action( 'delete_option', $option );
514 $result = $wpdb->delete( $wpdb->options, array( 'option_name' => $option ) );
515 if ( ! wp_installing() ) {
516 if ( 'yes' == $row->autoload ) {
517 $alloptions = wp_load_alloptions();
518 if ( is_array( $alloptions ) && isset( $alloptions[$option] ) ) {
519 unset( $alloptions[$option] );
520 wp_cache_set( 'alloptions', $alloptions, 'options' );
523 wp_cache_delete( $option, 'options' );
529 * Fires after a specific option has been deleted.
531 * The dynamic portion of the hook name, `$option`, refers to the option name.
535 * @param string $option Name of the deleted option.
537 do_action( "delete_option_$option", $option );
540 * Fires after an option has been deleted.
544 * @param string $option Name of the deleted option.
546 do_action( 'deleted_option', $option );
553 * Delete a transient.
557 * @param string $transient Transient name. Expected to not be SQL-escaped.
558 * @return bool true if successful, false otherwise
560 function delete_transient( $transient ) {
563 * Fires immediately before a specific transient is deleted.
565 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
569 * @param string $transient Transient name.
571 do_action( 'delete_transient_' . $transient, $transient );
573 if ( wp_using_ext_object_cache() ) {
574 $result = wp_cache_delete( $transient, 'transient' );
576 $option_timeout = '_transient_timeout_' . $transient;
577 $option = '_transient_' . $transient;
578 $result = delete_option( $option );
580 delete_option( $option_timeout );
586 * Fires after a transient is deleted.
590 * @param string $transient Deleted transient name.
592 do_action( 'deleted_transient', $transient );
599 * Get the value of a transient.
601 * If the transient does not exist, does not have a value, or has expired,
602 * then the return value will be false.
606 * @param string $transient Transient name. Expected to not be SQL-escaped.
607 * @return mixed Value of transient.
609 function get_transient( $transient ) {
612 * Filters the value of an existing transient.
614 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
616 * Passing a truthy value to the filter will effectively short-circuit retrieval
617 * of the transient, returning the passed value instead.
620 * @since 4.4.0 The `$transient` parameter was added
622 * @param mixed $pre_transient The default value to return if the transient does not exist.
623 * Any value other than false will short-circuit the retrieval
624 * of the transient, and return the returned value.
625 * @param string $transient Transient name.
627 $pre = apply_filters( 'pre_transient_' . $transient, false, $transient );
628 if ( false !== $pre )
631 if ( wp_using_ext_object_cache() ) {
632 $value = wp_cache_get( $transient, 'transient' );
634 $transient_option = '_transient_' . $transient;
635 if ( ! wp_installing() ) {
636 // If option is not in alloptions, it is not autoloaded and thus has a timeout
637 $alloptions = wp_load_alloptions();
638 if ( !isset( $alloptions[$transient_option] ) ) {
639 $transient_timeout = '_transient_timeout_' . $transient;
640 $timeout = get_option( $transient_timeout );
641 if ( false !== $timeout && $timeout < time() ) {
642 delete_option( $transient_option );
643 delete_option( $transient_timeout );
649 if ( ! isset( $value ) )
650 $value = get_option( $transient_option );
654 * Filters an existing transient's value.
656 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
659 * @since 4.4.0 The `$transient` parameter was added
661 * @param mixed $value Value of transient.
662 * @param string $transient Transient name.
664 return apply_filters( 'transient_' . $transient, $value, $transient );
668 * Set/update the value of a transient.
670 * You do not need to serialize values. If the value needs to be serialized, then
671 * it will be serialized before it is set.
675 * @param string $transient Transient name. Expected to not be SQL-escaped. Must be
676 * 172 characters or fewer in length.
677 * @param mixed $value Transient value. Must be serializable if non-scalar.
678 * Expected to not be SQL-escaped.
679 * @param int $expiration Optional. Time until expiration in seconds. Default 0 (no expiration).
680 * @return bool False if value was not set and true if value was set.
682 function set_transient( $transient, $value, $expiration = 0 ) {
684 $expiration = (int) $expiration;
687 * Filters a specific transient before its value is set.
689 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
692 * @since 4.2.0 The `$expiration` parameter was added.
693 * @since 4.4.0 The `$transient` parameter was added.
695 * @param mixed $value New value of transient.
696 * @param int $expiration Time until expiration in seconds.
697 * @param string $transient Transient name.
699 $value = apply_filters( 'pre_set_transient_' . $transient, $value, $expiration, $transient );
702 * Filters the expiration for a transient before its value is set.
704 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
708 * @param int $expiration Time until expiration in seconds. Use 0 for no expiration.
709 * @param mixed $value New value of transient.
710 * @param string $transient Transient name.
712 $expiration = apply_filters( 'expiration_of_transient_' . $transient, $expiration, $value, $transient );
714 if ( wp_using_ext_object_cache() ) {
715 $result = wp_cache_set( $transient, $value, 'transient', $expiration );
717 $transient_timeout = '_transient_timeout_' . $transient;
718 $transient_option = '_transient_' . $transient;
719 if ( false === get_option( $transient_option ) ) {
723 add_option( $transient_timeout, time() + $expiration, '', 'no' );
725 $result = add_option( $transient_option, $value, '', $autoload );
727 // If expiration is requested, but the transient has no timeout option,
728 // delete, then re-create transient rather than update.
731 if ( false === get_option( $transient_timeout ) ) {
732 delete_option( $transient_option );
733 add_option( $transient_timeout, time() + $expiration, '', 'no' );
734 $result = add_option( $transient_option, $value, '', 'no' );
737 update_option( $transient_timeout, time() + $expiration );
741 $result = update_option( $transient_option, $value );
749 * Fires after the value for a specific transient has been set.
751 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
754 * @since 3.6.0 The `$value` and `$expiration` parameters were added.
755 * @since 4.4.0 The `$transient` parameter was added.
757 * @param mixed $value Transient value.
758 * @param int $expiration Time until expiration in seconds.
759 * @param string $transient The name of the transient.
761 do_action( 'set_transient_' . $transient, $value, $expiration, $transient );
764 * Fires after the value for a transient has been set.
767 * @since 3.6.0 The `$value` and `$expiration` parameters were added.
769 * @param string $transient The name of the transient.
770 * @param mixed $value Transient value.
771 * @param int $expiration Time until expiration in seconds.
773 do_action( 'setted_transient', $transient, $value, $expiration );
779 * Saves and restores user interface settings stored in a cookie.
781 * Checks if the current user-settings cookie is updated and stores it. When no
782 * cookie exists (different browser used), adds the last saved cookie restoring
787 function wp_user_settings() {
789 if ( ! is_admin() || defined( 'DOING_AJAX' ) ) {
793 if ( ! $user_id = get_current_user_id() ) {
797 if ( is_super_admin() && ! is_user_member_of_blog() ) {
801 $settings = (string) get_user_option( 'user-settings', $user_id );
803 if ( isset( $_COOKIE['wp-settings-' . $user_id] ) ) {
804 $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user_id] );
806 // No change or both empty
807 if ( $cookie == $settings )
810 $last_saved = (int) get_user_option( 'user-settings-time', $user_id );
811 $current = isset( $_COOKIE['wp-settings-time-' . $user_id]) ? preg_replace( '/[^0-9]/', '', $_COOKIE['wp-settings-time-' . $user_id] ) : 0;
813 // The cookie is newer than the saved value. Update the user_option and leave the cookie as-is
814 if ( $current > $last_saved ) {
815 update_user_option( $user_id, 'user-settings', $cookie, false );
816 update_user_option( $user_id, 'user-settings-time', time() - 5, false );
821 // The cookie is not set in the current browser or the saved value is newer.
822 $secure = ( 'https' === parse_url( admin_url(), PHP_URL_SCHEME ) );
823 setcookie( 'wp-settings-' . $user_id, $settings, time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure );
824 setcookie( 'wp-settings-time-' . $user_id, time(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure );
825 $_COOKIE['wp-settings-' . $user_id] = $settings;
829 * Retrieve user interface setting value based on setting name.
833 * @param string $name The name of the setting.
834 * @param string $default Optional default value to return when $name is not set.
835 * @return mixed the last saved user setting or the default value/false if it doesn't exist.
837 function get_user_setting( $name, $default = false ) {
838 $all_user_settings = get_all_user_settings();
840 return isset( $all_user_settings[$name] ) ? $all_user_settings[$name] : $default;
844 * Add or update user interface setting.
846 * Both $name and $value can contain only ASCII letters, numbers and underscores.
848 * This function has to be used before any output has started as it calls setcookie().
852 * @param string $name The name of the setting.
853 * @param string $value The value for the setting.
854 * @return bool|null True if set successfully, false if not. Null if the current user can't be established.
856 function set_user_setting( $name, $value ) {
857 if ( headers_sent() ) {
861 $all_user_settings = get_all_user_settings();
862 $all_user_settings[$name] = $value;
864 return wp_set_all_user_settings( $all_user_settings );
868 * Delete user interface settings.
870 * Deleting settings would reset them to the defaults.
872 * This function has to be used before any output has started as it calls setcookie().
876 * @param string $names The name or array of names of the setting to be deleted.
877 * @return bool|null True if deleted successfully, false if not. Null if the current user can't be established.
879 function delete_user_setting( $names ) {
880 if ( headers_sent() ) {
884 $all_user_settings = get_all_user_settings();
885 $names = (array) $names;
888 foreach ( $names as $name ) {
889 if ( isset( $all_user_settings[$name] ) ) {
890 unset( $all_user_settings[$name] );
896 return wp_set_all_user_settings( $all_user_settings );
903 * Retrieve all user interface settings.
907 * @global array $_updated_user_settings
909 * @return array the last saved user settings or empty array.
911 function get_all_user_settings() {
912 global $_updated_user_settings;
914 if ( ! $user_id = get_current_user_id() ) {
918 if ( isset( $_updated_user_settings ) && is_array( $_updated_user_settings ) ) {
919 return $_updated_user_settings;
922 $user_settings = array();
924 if ( isset( $_COOKIE['wp-settings-' . $user_id] ) ) {
925 $cookie = preg_replace( '/[^A-Za-z0-9=&_-]/', '', $_COOKIE['wp-settings-' . $user_id] );
927 if ( strpos( $cookie, '=' ) ) { // '=' cannot be 1st char
928 parse_str( $cookie, $user_settings );
931 $option = get_user_option( 'user-settings', $user_id );
933 if ( $option && is_string( $option ) ) {
934 parse_str( $option, $user_settings );
938 $_updated_user_settings = $user_settings;
939 return $user_settings;
943 * Private. Set all user interface settings.
948 * @global array $_updated_user_settings
950 * @param array $user_settings User settings.
951 * @return bool|null False if the current user can't be found, null if the current
952 * user is not a super admin or a member of the site, otherwise true.
954 function wp_set_all_user_settings( $user_settings ) {
955 global $_updated_user_settings;
957 if ( ! $user_id = get_current_user_id() ) {
961 if ( is_super_admin() && ! is_user_member_of_blog() ) {
966 foreach ( $user_settings as $name => $value ) {
967 $_name = preg_replace( '/[^A-Za-z0-9_-]+/', '', $name );
968 $_value = preg_replace( '/[^A-Za-z0-9_-]+/', '', $value );
970 if ( ! empty( $_name ) ) {
971 $settings .= $_name . '=' . $_value . '&';
975 $settings = rtrim( $settings, '&' );
976 parse_str( $settings, $_updated_user_settings );
978 update_user_option( $user_id, 'user-settings', $settings, false );
979 update_user_option( $user_id, 'user-settings-time', time(), false );
985 * Delete the user settings of the current user.
989 function delete_all_user_settings() {
990 if ( ! $user_id = get_current_user_id() ) {
994 update_user_option( $user_id, 'user-settings', '', false );
995 setcookie( 'wp-settings-' . $user_id, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH );
999 * Retrieve an option value for the current network based on name of option.
1002 * @since 4.4.0 The `$use_cache` parameter was deprecated.
1003 * @since 4.4.0 Modified into wrapper for get_network_option()
1005 * @see get_network_option()
1007 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
1008 * @param mixed $default Optional value to return if option doesn't exist. Default false.
1009 * @param bool $deprecated Whether to use cache. Multisite only. Always set to true.
1010 * @return mixed Value set for the option.
1012 function get_site_option( $option, $default = false, $deprecated = true ) {
1013 return get_network_option( null, $option, $default );
1017 * Add a new option for the current network.
1019 * Existing options will not be updated. Note that prior to 3.3 this wasn't the case.
1022 * @since 4.4.0 Modified into wrapper for add_network_option()
1024 * @see add_network_option()
1026 * @param string $option Name of option to add. Expected to not be SQL-escaped.
1027 * @param mixed $value Option value, can be anything. Expected to not be SQL-escaped.
1028 * @return bool False if the option was not added. True if the option was added.
1030 function add_site_option( $option, $value ) {
1031 return add_network_option( null, $option, $value );
1035 * Removes a option by name for the current network.
1038 * @since 4.4.0 Modified into wrapper for delete_network_option()
1040 * @see delete_network_option()
1042 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
1043 * @return bool True, if succeed. False, if failure.
1045 function delete_site_option( $option ) {
1046 return delete_network_option( null, $option );
1050 * Update the value of an option that was already added for the current network.
1053 * @since 4.4.0 Modified into wrapper for update_network_option()
1055 * @see update_network_option()
1057 * @param string $option Name of option. Expected to not be SQL-escaped.
1058 * @param mixed $value Option value. Expected to not be SQL-escaped.
1059 * @return bool False if value was not updated. True if value was updated.
1061 function update_site_option( $option, $value ) {
1062 return update_network_option( null, $option, $value );
1066 * Retrieve a network's option value based on the option name.
1072 * @global wpdb $wpdb
1073 * @global object $current_site
1075 * @param int $network_id ID of the network. Can be null to default to the current network ID.
1076 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
1077 * @param mixed $default Optional. Value to return if the option doesn't exist. Default false.
1078 * @return mixed Value set for the option.
1080 function get_network_option( $network_id, $option, $default = false ) {
1081 global $wpdb, $current_site;
1083 if ( $network_id && ! is_numeric( $network_id ) ) {
1087 $network_id = (int) $network_id;
1089 // Fallback to the current network if a network ID is not specified.
1090 if ( ! $network_id && is_multisite() ) {
1091 $network_id = $current_site->id;
1095 * Filters an existing network option before it is retrieved.
1097 * The dynamic portion of the hook name, `$option`, refers to the option name.
1099 * Passing a truthy value to the filter will effectively short-circuit retrieval,
1100 * returning the passed value instead.
1102 * @since 2.9.0 As 'pre_site_option_' . $key
1104 * @since 4.4.0 The `$option` parameter was added
1106 * @param mixed $pre_option The default value to return if the option does not exist.
1107 * @param string $option Option name.
1109 $pre = apply_filters( 'pre_site_option_' . $option, false, $option );
1111 if ( false !== $pre ) {
1115 // prevent non-existent options from triggering multiple queries
1116 $notoptions_key = "$network_id:notoptions";
1117 $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
1119 if ( isset( $notoptions[ $option ] ) ) {
1122 * Filters a specific default network option.
1124 * The dynamic portion of the hook name, `$option`, refers to the option name.
1127 * @since 4.4.0 The `$option` parameter was added.
1129 * @param mixed $default The value to return if the site option does not exist
1131 * @param string $option Option name.
1133 return apply_filters( 'default_site_option_' . $option, $default, $option );
1136 if ( ! is_multisite() ) {
1137 /** This filter is documented in wp-includes/option.php */
1138 $default = apply_filters( 'default_site_option_' . $option, $default, $option );
1139 $value = get_option( $option, $default );
1141 $cache_key = "$network_id:$option";
1142 $value = wp_cache_get( $cache_key, 'site-options' );
1144 if ( ! isset( $value ) || false === $value ) {
1145 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $network_id ) );
1147 // Has to be get_row instead of get_var because of funkiness with 0, false, null values
1148 if ( is_object( $row ) ) {
1149 $value = $row->meta_value;
1150 $value = maybe_unserialize( $value );
1151 wp_cache_set( $cache_key, $value, 'site-options' );
1153 if ( ! is_array( $notoptions ) ) {
1154 $notoptions = array();
1156 $notoptions[ $option ] = true;
1157 wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
1159 /** This filter is documented in wp-includes/option.php */
1160 $value = apply_filters( 'default_site_option_' . $option, $default, $option );
1166 * Filters the value of an existing network option.
1168 * The dynamic portion of the hook name, `$option`, refers to the option name.
1170 * @since 2.9.0 As 'site_option_' . $key
1172 * @since 4.4.0 The `$option` parameter was added
1174 * @param mixed $value Value of network option.
1175 * @param string $option Option name.
1177 return apply_filters( 'site_option_' . $option, $value, $option );
1181 * Add a new network option.
1183 * Existing options will not be updated.
1189 * @global wpdb $wpdb
1190 * @global object $current_site
1192 * @param int $network_id ID of the network. Can be null to default to the current network ID.
1193 * @param string $option Name of option to add. Expected to not be SQL-escaped.
1194 * @param mixed $value Option value, can be anything. Expected to not be SQL-escaped.
1195 * @return bool False if option was not added and true if option was added.
1197 function add_network_option( $network_id, $option, $value ) {
1198 global $wpdb, $current_site;
1200 if ( $network_id && ! is_numeric( $network_id ) ) {
1204 $network_id = (int) $network_id;
1206 // Fallback to the current network if a network ID is not specified.
1207 if ( ! $network_id && is_multisite() ) {
1208 $network_id = $current_site->id;
1211 wp_protect_special_option( $option );
1214 * Filters the value of a specific network option before it is added.
1216 * The dynamic portion of the hook name, `$option`, refers to the option name.
1218 * @since 2.9.0 As 'pre_add_site_option_' . $key
1220 * @since 4.4.0 The `$option` parameter was added
1222 * @param mixed $value Value of network option.
1223 * @param string $option Option name.
1225 $value = apply_filters( 'pre_add_site_option_' . $option, $value, $option );
1227 $notoptions_key = "$network_id:notoptions";
1229 if ( ! is_multisite() ) {
1230 $result = add_option( $option, $value, '', 'no' );
1232 $cache_key = "$network_id:$option";
1234 // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
1235 $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
1236 if ( ! is_array( $notoptions ) || ! isset( $notoptions[ $option ] ) ) {
1237 if ( false !== get_network_option( $network_id, $option, false ) ) {
1242 $value = sanitize_option( $option, $value );
1244 $serialized_value = maybe_serialize( $value );
1245 $result = $wpdb->insert( $wpdb->sitemeta, array( 'site_id' => $network_id, 'meta_key' => $option, 'meta_value' => $serialized_value ) );
1251 wp_cache_set( $cache_key, $value, 'site-options' );
1253 // This option exists now
1254 $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); // yes, again... we need it to be fresh
1255 if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
1256 unset( $notoptions[ $option ] );
1257 wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
1264 * Fires after a specific network option has been successfully added.
1266 * The dynamic portion of the hook name, `$option`, refers to the option name.
1268 * @since 2.9.0 As "add_site_option_{$key}"
1271 * @param string $option Name of the network option.
1272 * @param mixed $value Value of the network option.
1274 do_action( 'add_site_option_' . $option, $option, $value );
1277 * Fires after a network option has been successfully added.
1281 * @param string $option Name of the network option.
1282 * @param mixed $value Value of the network option.
1284 do_action( 'add_site_option', $option, $value );
1293 * Removes a network option by name.
1297 * @see delete_option()
1299 * @global wpdb $wpdb
1300 * @global object $current_site
1302 * @param int $network_id ID of the network. Can be null to default to the current network ID.
1303 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
1304 * @return bool True, if succeed. False, if failure.
1306 function delete_network_option( $network_id, $option ) {
1307 global $wpdb, $current_site;
1309 if ( $network_id && ! is_numeric( $network_id ) ) {
1313 $network_id = (int) $network_id;
1315 // Fallback to the current network if a network ID is not specified.
1316 if ( ! $network_id && is_multisite() ) {
1317 $network_id = $current_site->id;
1321 * Fires immediately before a specific network option is deleted.
1323 * The dynamic portion of the hook name, `$option`, refers to the option name.
1326 * @since 4.4.0 The `$option` parameter was added
1328 * @param string $option Option name.
1330 do_action( 'pre_delete_site_option_' . $option, $option );
1332 if ( ! is_multisite() ) {
1333 $result = delete_option( $option );
1335 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $network_id ) );
1336 if ( is_null( $row ) || ! $row->meta_id ) {
1339 $cache_key = "$network_id:$option";
1340 wp_cache_delete( $cache_key, 'site-options' );
1342 $result = $wpdb->delete( $wpdb->sitemeta, array( 'meta_key' => $option, 'site_id' => $network_id ) );
1348 * Fires after a specific network option has been deleted.
1350 * The dynamic portion of the hook name, `$option`, refers to the option name.
1352 * @since 2.9.0 As "delete_site_option_{$key}"
1355 * @param string $option Name of the network option.
1357 do_action( 'delete_site_option_' . $option, $option );
1360 * Fires after a network option has been deleted.
1364 * @param string $option Name of the network option.
1366 do_action( 'delete_site_option', $option );
1375 * Update the value of a network option that was already added.
1379 * @see update_option()
1381 * @global wpdb $wpdb
1382 * @global object $current_site
1384 * @param int $network_id ID of the network. Can be null to default to the current network ID.
1385 * @param string $option Name of option. Expected to not be SQL-escaped.
1386 * @param mixed $value Option value. Expected to not be SQL-escaped.
1387 * @return bool False if value was not updated and true if value was updated.
1389 function update_network_option( $network_id, $option, $value ) {
1390 global $wpdb, $current_site;
1392 if ( $network_id && ! is_numeric( $network_id ) ) {
1396 $network_id = (int) $network_id;
1398 // Fallback to the current network if a network ID is not specified.
1399 if ( ! $network_id && is_multisite() ) {
1400 $network_id = $current_site->id;
1403 wp_protect_special_option( $option );
1405 $old_value = get_network_option( $network_id, $option, false );
1408 * Filters a specific network option before its value is updated.
1410 * The dynamic portion of the hook name, `$option`, refers to the option name.
1412 * @since 2.9.0 As 'pre_update_site_option_' . $key
1414 * @since 4.4.0 The `$option` parameter was added
1416 * @param mixed $value New value of the network option.
1417 * @param mixed $old_value Old value of the network option.
1418 * @param string $option Option name.
1420 $value = apply_filters( 'pre_update_site_option_' . $option, $value, $old_value, $option );
1422 if ( $value === $old_value ) {
1426 if ( false === $old_value ) {
1427 return add_network_option( $network_id, $option, $value );
1430 $notoptions_key = "$network_id:notoptions";
1431 $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
1432 if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
1433 unset( $notoptions[ $option ] );
1434 wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
1437 if ( ! is_multisite() ) {
1438 $result = update_option( $option, $value, 'no' );
1440 $value = sanitize_option( $option, $value );
1442 $serialized_value = maybe_serialize( $value );
1443 $result = $wpdb->update( $wpdb->sitemeta, array( 'meta_value' => $serialized_value ), array( 'site_id' => $network_id, 'meta_key' => $option ) );
1446 $cache_key = "$network_id:$option";
1447 wp_cache_set( $cache_key, $value, 'site-options' );
1454 * Fires after the value of a specific network option has been successfully updated.
1456 * The dynamic portion of the hook name, `$option`, refers to the option name.
1458 * @since 2.9.0 As "update_site_option_{$key}"
1461 * @param string $option Name of the network option.
1462 * @param mixed $value Current value of the network option.
1463 * @param mixed $old_value Old value of the network option.
1465 do_action( 'update_site_option_' . $option, $option, $value, $old_value );
1468 * Fires after the value of a network option has been successfully updated.
1472 * @param string $option Name of the network option.
1473 * @param mixed $value Current value of the network option.
1474 * @param mixed $old_value Old value of the network option.
1476 do_action( 'update_site_option', $option, $value, $old_value );
1485 * Delete a site transient.
1489 * @param string $transient Transient name. Expected to not be SQL-escaped.
1490 * @return bool True if successful, false otherwise
1492 function delete_site_transient( $transient ) {
1495 * Fires immediately before a specific site transient is deleted.
1497 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1501 * @param string $transient Transient name.
1503 do_action( 'delete_site_transient_' . $transient, $transient );
1505 if ( wp_using_ext_object_cache() ) {
1506 $result = wp_cache_delete( $transient, 'site-transient' );
1508 $option_timeout = '_site_transient_timeout_' . $transient;
1509 $option = '_site_transient_' . $transient;
1510 $result = delete_site_option( $option );
1512 delete_site_option( $option_timeout );
1517 * Fires after a transient is deleted.
1521 * @param string $transient Deleted transient name.
1523 do_action( 'deleted_site_transient', $transient );
1530 * Get the value of a site transient.
1532 * If the transient does not exist, does not have a value, or has expired,
1533 * then the return value will be false.
1537 * @see get_transient()
1539 * @param string $transient Transient name. Expected to not be SQL-escaped.
1540 * @return mixed Value of transient.
1542 function get_site_transient( $transient ) {
1545 * Filters the value of an existing site transient.
1547 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1549 * Passing a truthy value to the filter will effectively short-circuit retrieval,
1550 * returning the passed value instead.
1553 * @since 4.4.0 The `$transient` parameter was added.
1555 * @param mixed $pre_site_transient The default value to return if the site transient does not exist.
1556 * Any value other than false will short-circuit the retrieval
1557 * of the transient, and return the returned value.
1558 * @param string $transient Transient name.
1560 $pre = apply_filters( 'pre_site_transient_' . $transient, false, $transient );
1562 if ( false !== $pre )
1565 if ( wp_using_ext_object_cache() ) {
1566 $value = wp_cache_get( $transient, 'site-transient' );
1568 // Core transients that do not have a timeout. Listed here so querying timeouts can be avoided.
1569 $no_timeout = array('update_core', 'update_plugins', 'update_themes');
1570 $transient_option = '_site_transient_' . $transient;
1571 if ( ! in_array( $transient, $no_timeout ) ) {
1572 $transient_timeout = '_site_transient_timeout_' . $transient;
1573 $timeout = get_site_option( $transient_timeout );
1574 if ( false !== $timeout && $timeout < time() ) {
1575 delete_site_option( $transient_option );
1576 delete_site_option( $transient_timeout );
1581 if ( ! isset( $value ) )
1582 $value = get_site_option( $transient_option );
1586 * Filters the value of an existing site transient.
1588 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1591 * @since 4.4.0 The `$transient` parameter was added.
1593 * @param mixed $value Value of site transient.
1594 * @param string $transient Transient name.
1596 return apply_filters( 'site_transient_' . $transient, $value, $transient );
1600 * Set/update the value of a site transient.
1602 * You do not need to serialize values, if the value needs to be serialize, then
1603 * it will be serialized before it is set.
1607 * @see set_transient()
1609 * @param string $transient Transient name. Expected to not be SQL-escaped. Must be
1610 * 40 characters or fewer in length.
1611 * @param mixed $value Transient value. Expected to not be SQL-escaped.
1612 * @param int $expiration Optional. Time until expiration in seconds. Default 0 (no expiration).
1613 * @return bool False if value was not set and true if value was set.
1615 function set_site_transient( $transient, $value, $expiration = 0 ) {
1618 * Filters the value of a specific site transient before it is set.
1620 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1623 * @since 4.4.0 The `$transient` parameter was added.
1625 * @param mixed $value New value of site transient.
1626 * @param string $transient Transient name.
1628 $value = apply_filters( 'pre_set_site_transient_' . $transient, $value, $transient );
1630 $expiration = (int) $expiration;
1633 * Filters the expiration for a site transient before its value is set.
1635 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1639 * @param int $expiration Time until expiration in seconds. Use 0 for no expiration.
1640 * @param mixed $value New value of site transient.
1641 * @param string $transient Transient name.
1643 $expiration = apply_filters( 'expiration_of_site_transient_' . $transient, $expiration, $value, $transient );
1645 if ( wp_using_ext_object_cache() ) {
1646 $result = wp_cache_set( $transient, $value, 'site-transient', $expiration );
1648 $transient_timeout = '_site_transient_timeout_' . $transient;
1649 $option = '_site_transient_' . $transient;
1650 if ( false === get_site_option( $option ) ) {
1652 add_site_option( $transient_timeout, time() + $expiration );
1653 $result = add_site_option( $option, $value );
1656 update_site_option( $transient_timeout, time() + $expiration );
1657 $result = update_site_option( $option, $value );
1663 * Fires after the value for a specific site transient has been set.
1665 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1668 * @since 4.4.0 The `$transient` parameter was added
1670 * @param mixed $value Site transient value.
1671 * @param int $expiration Time until expiration in seconds.
1672 * @param string $transient Transient name.
1674 do_action( 'set_site_transient_' . $transient, $value, $expiration, $transient );
1677 * Fires after the value for a site transient has been set.
1681 * @param string $transient The name of the site transient.
1682 * @param mixed $value Site transient value.
1683 * @param int $expiration Time until expiration in seconds.
1685 do_action( 'setted_site_transient', $transient, $value, $expiration );