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.
19 * Any scalar values will be returned as strings. You may coerce the return type of a given option by registering a
20 * '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 * Filter 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 * Filter 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 * Filter 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 * Filter 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 * Filter 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 ) === $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' );
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 ) !== 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 * Filter 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 * Filter 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 * Filter 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 * Filter 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.
847 * This function has to be used before any output has started as it calls setcookie().
851 * @param string $name The name of the setting.
852 * @param string $value The value for the setting.
853 * @return bool|void true if set successfully/false if not.
855 function set_user_setting( $name, $value ) {
856 if ( headers_sent() ) {
860 $all_user_settings = get_all_user_settings();
861 $all_user_settings[$name] = $value;
863 return wp_set_all_user_settings( $all_user_settings );
867 * Delete user interface settings.
869 * Deleting settings would reset them to the defaults.
870 * This function has to be used before any output has started as it calls setcookie().
874 * @param string $names The name or array of names of the setting to be deleted.
875 * @return bool|void true if deleted successfully/false if not.
877 function delete_user_setting( $names ) {
878 if ( headers_sent() ) {
882 $all_user_settings = get_all_user_settings();
883 $names = (array) $names;
886 foreach ( $names as $name ) {
887 if ( isset( $all_user_settings[$name] ) ) {
888 unset( $all_user_settings[$name] );
894 return wp_set_all_user_settings( $all_user_settings );
901 * Retrieve all user interface settings.
905 * @global array $_updated_user_settings
907 * @return array the last saved user settings or empty array.
909 function get_all_user_settings() {
910 global $_updated_user_settings;
912 if ( ! $user_id = get_current_user_id() ) {
916 if ( isset( $_updated_user_settings ) && is_array( $_updated_user_settings ) ) {
917 return $_updated_user_settings;
920 $user_settings = array();
922 if ( isset( $_COOKIE['wp-settings-' . $user_id] ) ) {
923 $cookie = preg_replace( '/[^A-Za-z0-9=&_-]/', '', $_COOKIE['wp-settings-' . $user_id] );
925 if ( strpos( $cookie, '=' ) ) { // '=' cannot be 1st char
926 parse_str( $cookie, $user_settings );
929 $option = get_user_option( 'user-settings', $user_id );
931 if ( $option && is_string( $option ) ) {
932 parse_str( $option, $user_settings );
936 $_updated_user_settings = $user_settings;
937 return $user_settings;
941 * Private. Set all user interface settings.
945 * @global array $_updated_user_settings
947 * @param array $user_settings
950 function wp_set_all_user_settings( $user_settings ) {
951 global $_updated_user_settings;
953 if ( ! $user_id = get_current_user_id() ) {
957 if ( is_super_admin() && ! is_user_member_of_blog() ) {
962 foreach ( $user_settings as $name => $value ) {
963 $_name = preg_replace( '/[^A-Za-z0-9_-]+/', '', $name );
964 $_value = preg_replace( '/[^A-Za-z0-9_-]+/', '', $value );
966 if ( ! empty( $_name ) ) {
967 $settings .= $_name . '=' . $_value . '&';
971 $settings = rtrim( $settings, '&' );
972 parse_str( $settings, $_updated_user_settings );
974 update_user_option( $user_id, 'user-settings', $settings, false );
975 update_user_option( $user_id, 'user-settings-time', time(), false );
981 * Delete the user settings of the current user.
985 function delete_all_user_settings() {
986 if ( ! $user_id = get_current_user_id() ) {
990 update_user_option( $user_id, 'user-settings', '', false );
991 setcookie( 'wp-settings-' . $user_id, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH );
995 * Retrieve an option value for the current network based on name of option.
998 * @since 4.4.0 The `$use_cache` parameter was deprecated.
999 * @since 4.4.0 Modified into wrapper for get_network_option()
1001 * @see get_network_option()
1003 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
1004 * @param mixed $default Optional value to return if option doesn't exist. Default false.
1005 * @param bool $deprecated Whether to use cache. Multisite only. Always set to true.
1006 * @return mixed Value set for the option.
1008 function get_site_option( $option, $default = false, $deprecated = true ) {
1009 return get_network_option( null, $option, $default );
1013 * Add a new option for the current network.
1015 * Existing options will not be updated. Note that prior to 3.3 this wasn't the case.
1018 * @since 4.4.0 Modified into wrapper for add_network_option()
1020 * @see add_network_option()
1022 * @param string $option Name of option to add. Expected to not be SQL-escaped.
1023 * @param mixed $value Option value, can be anything. Expected to not be SQL-escaped.
1024 * @return bool False if the option was not added. True if the option was added.
1026 function add_site_option( $option, $value ) {
1027 return add_network_option( null, $option, $value );
1031 * Removes a option by name for the current network.
1034 * @since 4.4.0 Modified into wrapper for delete_network_option()
1036 * @see delete_network_option()
1038 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
1039 * @return bool True, if succeed. False, if failure.
1041 function delete_site_option( $option ) {
1042 return delete_network_option( null, $option );
1046 * Update the value of an option that was already added for the current network.
1049 * @since 4.4.0 Modified into wrapper for update_network_option()
1051 * @see update_network_option()
1053 * @param string $option Name of option. Expected to not be SQL-escaped.
1054 * @param mixed $value Option value. Expected to not be SQL-escaped.
1055 * @return bool False if value was not updated. True if value was updated.
1057 function update_site_option( $option, $value ) {
1058 return update_network_option( null, $option, $value );
1062 * Retrieve a network's option value based on the option name.
1068 * @global wpdb $wpdb
1069 * @global object $current_site
1071 * @param int $network_id ID of the network. Can be null to default to the current network ID.
1072 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
1073 * @param mixed $default Optional. Value to return if the option doesn't exist. Default false.
1074 * @return mixed Value set for the option.
1076 function get_network_option( $network_id, $option, $default = false ) {
1077 global $wpdb, $current_site;
1079 if ( $network_id && ! is_numeric( $network_id ) ) {
1083 $network_id = (int) $network_id;
1085 // Fallback to the current network if a network ID is not specified.
1086 if ( ! $network_id && is_multisite() ) {
1087 $network_id = $current_site->id;
1091 * Filter an existing network option before it is retrieved.
1093 * The dynamic portion of the hook name, `$option`, refers to the option name.
1095 * Passing a truthy value to the filter will effectively short-circuit retrieval,
1096 * returning the passed value instead.
1098 * @since 2.9.0 As 'pre_site_option_' . $key
1100 * @since 4.4.0 The `$option` parameter was added
1102 * @param mixed $pre_option The default value to return if the option does not exist.
1103 * @param string $option Option name.
1105 $pre = apply_filters( 'pre_site_option_' . $option, false, $option );
1107 if ( false !== $pre ) {
1111 // prevent non-existent options from triggering multiple queries
1112 $notoptions_key = "$network_id:notoptions";
1113 $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
1115 if ( isset( $notoptions[ $option ] ) ) {
1118 * Filter a specific default network option.
1120 * The dynamic portion of the hook name, `$option`, refers to the option name.
1123 * @since 4.4.0 The `$option` parameter was added.
1125 * @param mixed $default The value to return if the site option does not exist
1127 * @param string $option Option name.
1129 return apply_filters( 'default_site_option_' . $option, $default, $option );
1132 if ( ! is_multisite() ) {
1133 /** This filter is documented in wp-includes/option.php */
1134 $default = apply_filters( 'default_site_option_' . $option, $default, $option );
1135 $value = get_option( $option, $default );
1137 $cache_key = "$network_id:$option";
1138 $value = wp_cache_get( $cache_key, 'site-options' );
1140 if ( ! isset( $value ) || false === $value ) {
1141 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $network_id ) );
1143 // Has to be get_row instead of get_var because of funkiness with 0, false, null values
1144 if ( is_object( $row ) ) {
1145 $value = $row->meta_value;
1146 $value = maybe_unserialize( $value );
1147 wp_cache_set( $cache_key, $value, 'site-options' );
1149 if ( ! is_array( $notoptions ) ) {
1150 $notoptions = array();
1152 $notoptions[ $option ] = true;
1153 wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
1155 /** This filter is documented in wp-includes/option.php */
1156 $value = apply_filters( 'default_site_option_' . $option, $default, $option );
1162 * Filter the value of an existing network option.
1164 * The dynamic portion of the hook name, `$option`, refers to the option name.
1166 * @since 2.9.0 As 'site_option_' . $key
1168 * @since 4.4.0 The `$option` parameter was added
1170 * @param mixed $value Value of network option.
1171 * @param string $option Option name.
1173 return apply_filters( 'site_option_' . $option, $value, $option );
1177 * Add a new network option.
1179 * Existing options will not be updated.
1185 * @global wpdb $wpdb
1186 * @global object $current_site
1188 * @param int $network_id ID of the network. Can be null to default to the current network ID.
1189 * @param string $option Name of option to add. Expected to not be SQL-escaped.
1190 * @param mixed $value Option value, can be anything. Expected to not be SQL-escaped.
1191 * @return bool False if option was not added and true if option was added.
1193 function add_network_option( $network_id, $option, $value ) {
1194 global $wpdb, $current_site;
1196 if ( $network_id && ! is_numeric( $network_id ) ) {
1200 $network_id = (int) $network_id;
1202 // Fallback to the current network if a network ID is not specified.
1203 if ( ! $network_id && is_multisite() ) {
1204 $network_id = $current_site->id;
1207 wp_protect_special_option( $option );
1210 * Filter the value of a specific network option before it is added.
1212 * The dynamic portion of the hook name, `$option`, refers to the option name.
1214 * @since 2.9.0 As 'pre_add_site_option_' . $key
1216 * @since 4.4.0 The `$option` parameter was added
1218 * @param mixed $value Value of network option.
1219 * @param string $option Option name.
1221 $value = apply_filters( 'pre_add_site_option_' . $option, $value, $option );
1223 $notoptions_key = "$network_id:notoptions";
1225 if ( ! is_multisite() ) {
1226 $result = add_option( $option, $value );
1228 $cache_key = "$network_id:$option";
1230 // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
1231 $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
1232 if ( ! is_array( $notoptions ) || ! isset( $notoptions[ $option ] ) ) {
1233 if ( false !== get_network_option( $network_id, $option, false ) ) {
1238 $value = sanitize_option( $option, $value );
1240 $serialized_value = maybe_serialize( $value );
1241 $result = $wpdb->insert( $wpdb->sitemeta, array( 'site_id' => $network_id, 'meta_key' => $option, 'meta_value' => $serialized_value ) );
1247 wp_cache_set( $cache_key, $value, 'site-options' );
1249 // This option exists now
1250 $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); // yes, again... we need it to be fresh
1251 if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
1252 unset( $notoptions[ $option ] );
1253 wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
1260 * Fires after a specific network option has been successfully added.
1262 * The dynamic portion of the hook name, `$option`, refers to the option name.
1264 * @since 2.9.0 As "add_site_option_{$key}"
1267 * @param string $option Name of the network option.
1268 * @param mixed $value Value of the network option.
1270 do_action( 'add_site_option_' . $option, $option, $value );
1273 * Fires after a network option has been successfully added.
1277 * @param string $option Name of the network option.
1278 * @param mixed $value Value of the network option.
1280 do_action( 'add_site_option', $option, $value );
1289 * Removes a network option by name.
1293 * @see delete_option()
1295 * @global wpdb $wpdb
1296 * @global object $current_site
1298 * @param int $network_id ID of the network. Can be null to default to the current network ID.
1299 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
1300 * @return bool True, if succeed. False, if failure.
1302 function delete_network_option( $network_id, $option ) {
1303 global $wpdb, $current_site;
1305 if ( $network_id && ! is_numeric( $network_id ) ) {
1309 $network_id = (int) $network_id;
1311 // Fallback to the current network if a network ID is not specified.
1312 if ( ! $network_id && is_multisite() ) {
1313 $network_id = $current_site->id;
1317 * Fires immediately before a specific network option is deleted.
1319 * The dynamic portion of the hook name, `$option`, refers to the option name.
1322 * @since 4.4.0 The `$option` parameter was added
1324 * @param string $option Option name.
1326 do_action( 'pre_delete_site_option_' . $option, $option );
1328 if ( ! is_multisite() ) {
1329 $result = delete_option( $option );
1331 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $network_id ) );
1332 if ( is_null( $row ) || ! $row->meta_id ) {
1335 $cache_key = "$network_id:$option";
1336 wp_cache_delete( $cache_key, 'site-options' );
1338 $result = $wpdb->delete( $wpdb->sitemeta, array( 'meta_key' => $option, 'site_id' => $network_id ) );
1344 * Fires after a specific network option has been deleted.
1346 * The dynamic portion of the hook name, `$option`, refers to the option name.
1348 * @since 2.9.0 As "delete_site_option_{$key}"
1351 * @param string $option Name of the network option.
1353 do_action( 'delete_site_option_' . $option, $option );
1356 * Fires after a network option has been deleted.
1360 * @param string $option Name of the network option.
1362 do_action( 'delete_site_option', $option );
1371 * Update the value of a network option that was already added.
1375 * @see update_option()
1377 * @global wpdb $wpdb
1378 * @global object $current_site
1380 * @param int $network_id ID of the network. Can be null to default to the current network ID.
1381 * @param string $option Name of option. Expected to not be SQL-escaped.
1382 * @param mixed $value Option value. Expected to not be SQL-escaped.
1383 * @return bool False if value was not updated and true if value was updated.
1385 function update_network_option( $network_id, $option, $value ) {
1386 global $wpdb, $current_site;
1388 if ( $network_id && ! is_numeric( $network_id ) ) {
1392 $network_id = (int) $network_id;
1394 // Fallback to the current network if a network ID is not specified.
1395 if ( ! $network_id && is_multisite() ) {
1396 $network_id = $current_site->id;
1399 wp_protect_special_option( $option );
1401 $old_value = get_network_option( $network_id, $option, false );
1404 * Filter a specific network option before its value is updated.
1406 * The dynamic portion of the hook name, `$option`, refers to the option name.
1408 * @since 2.9.0 As 'pre_update_site_option_' . $key
1410 * @since 4.4.0 The `$option` parameter was added
1412 * @param mixed $value New value of the network option.
1413 * @param mixed $old_value Old value of the network option.
1414 * @param string $option Option name.
1416 $value = apply_filters( 'pre_update_site_option_' . $option, $value, $old_value, $option );
1418 if ( $value === $old_value ) {
1422 if ( false === $old_value ) {
1423 return add_network_option( $network_id, $option, $value );
1426 $notoptions_key = "$network_id:notoptions";
1427 $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
1428 if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
1429 unset( $notoptions[ $option ] );
1430 wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
1433 if ( ! is_multisite() ) {
1434 $result = update_option( $option, $value );
1436 $value = sanitize_option( $option, $value );
1438 $serialized_value = maybe_serialize( $value );
1439 $result = $wpdb->update( $wpdb->sitemeta, array( 'meta_value' => $serialized_value ), array( 'site_id' => $network_id, 'meta_key' => $option ) );
1442 $cache_key = "$network_id:$option";
1443 wp_cache_set( $cache_key, $value, 'site-options' );
1450 * Fires after the value of a specific network option has been successfully updated.
1452 * The dynamic portion of the hook name, `$option`, refers to the option name.
1454 * @since 2.9.0 As "update_site_option_{$key}"
1457 * @param string $option Name of the network option.
1458 * @param mixed $value Current value of the network option.
1459 * @param mixed $old_value Old value of the network option.
1461 do_action( 'update_site_option_' . $option, $option, $value, $old_value );
1464 * Fires after the value of a network option has been successfully updated.
1468 * @param string $option Name of the network option.
1469 * @param mixed $value Current value of the network option.
1470 * @param mixed $old_value Old value of the network option.
1472 do_action( 'update_site_option', $option, $value, $old_value );
1481 * Delete a site transient.
1485 * @param string $transient Transient name. Expected to not be SQL-escaped.
1486 * @return bool True if successful, false otherwise
1488 function delete_site_transient( $transient ) {
1491 * Fires immediately before a specific site transient is deleted.
1493 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1497 * @param string $transient Transient name.
1499 do_action( 'delete_site_transient_' . $transient, $transient );
1501 if ( wp_using_ext_object_cache() ) {
1502 $result = wp_cache_delete( $transient, 'site-transient' );
1504 $option_timeout = '_site_transient_timeout_' . $transient;
1505 $option = '_site_transient_' . $transient;
1506 $result = delete_site_option( $option );
1508 delete_site_option( $option_timeout );
1513 * Fires after a transient is deleted.
1517 * @param string $transient Deleted transient name.
1519 do_action( 'deleted_site_transient', $transient );
1526 * Get the value of a site transient.
1528 * If the transient does not exist, does not have a value, or has expired,
1529 * then the return value will be false.
1533 * @see get_transient()
1535 * @param string $transient Transient name. Expected to not be SQL-escaped.
1536 * @return mixed Value of transient.
1538 function get_site_transient( $transient ) {
1541 * Filter the value of an existing site transient.
1543 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1545 * Passing a truthy value to the filter will effectively short-circuit retrieval,
1546 * returning the passed value instead.
1549 * @since 4.4.0 The `$transient` parameter was added.
1551 * @param mixed $pre_site_transient The default value to return if the site transient does not exist.
1552 * Any value other than false will short-circuit the retrieval
1553 * of the transient, and return the returned value.
1554 * @param string $transient Transient name.
1556 $pre = apply_filters( 'pre_site_transient_' . $transient, false, $transient );
1558 if ( false !== $pre )
1561 if ( wp_using_ext_object_cache() ) {
1562 $value = wp_cache_get( $transient, 'site-transient' );
1564 // Core transients that do not have a timeout. Listed here so querying timeouts can be avoided.
1565 $no_timeout = array('update_core', 'update_plugins', 'update_themes');
1566 $transient_option = '_site_transient_' . $transient;
1567 if ( ! in_array( $transient, $no_timeout ) ) {
1568 $transient_timeout = '_site_transient_timeout_' . $transient;
1569 $timeout = get_site_option( $transient_timeout );
1570 if ( false !== $timeout && $timeout < time() ) {
1571 delete_site_option( $transient_option );
1572 delete_site_option( $transient_timeout );
1577 if ( ! isset( $value ) )
1578 $value = get_site_option( $transient_option );
1582 * Filter the value of an existing site transient.
1584 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1587 * @since 4.4.0 The `$transient` parameter was added.
1589 * @param mixed $value Value of site transient.
1590 * @param string $transient Transient name.
1592 return apply_filters( 'site_transient_' . $transient, $value, $transient );
1596 * Set/update the value of a site transient.
1598 * You do not need to serialize values, if the value needs to be serialize, then
1599 * it will be serialized before it is set.
1603 * @see set_transient()
1605 * @param string $transient Transient name. Expected to not be SQL-escaped. Must be
1606 * 40 characters or fewer in length.
1607 * @param mixed $value Transient value. Expected to not be SQL-escaped.
1608 * @param int $expiration Optional. Time until expiration in seconds. Default 0 (no expiration).
1609 * @return bool False if value was not set and true if value was set.
1611 function set_site_transient( $transient, $value, $expiration = 0 ) {
1614 * Filter the value of a specific site transient before it is set.
1616 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1619 * @since 4.4.0 The `$transient` parameter was added.
1621 * @param mixed $value New value of site transient.
1622 * @param string $transient Transient name.
1624 $value = apply_filters( 'pre_set_site_transient_' . $transient, $value, $transient );
1626 $expiration = (int) $expiration;
1629 * Filter the expiration for a site transient before its value is set.
1631 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1635 * @param int $expiration Time until expiration in seconds. Use 0 for no expiration.
1636 * @param mixed $value New value of site transient.
1637 * @param string $transient Transient name.
1639 $expiration = apply_filters( 'expiration_of_site_transient_' . $transient, $expiration, $value, $transient );
1641 if ( wp_using_ext_object_cache() ) {
1642 $result = wp_cache_set( $transient, $value, 'site-transient', $expiration );
1644 $transient_timeout = '_site_transient_timeout_' . $transient;
1645 $option = '_site_transient_' . $transient;
1646 if ( false === get_site_option( $option ) ) {
1648 add_site_option( $transient_timeout, time() + $expiration );
1649 $result = add_site_option( $option, $value );
1652 update_site_option( $transient_timeout, time() + $expiration );
1653 $result = update_site_option( $option, $value );
1659 * Fires after the value for a specific site transient has been set.
1661 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1664 * @since 4.4.0 The `$transient` parameter was added
1666 * @param mixed $value Site transient value.
1667 * @param int $expiration Time until expiration in seconds.
1668 * @param string $transient Transient name.
1670 do_action( 'set_site_transient_' . $transient, $value, $expiration, $transient );
1673 * Fires after the value for a site transient has been set.
1677 * @param string $transient The name of the site transient.
1678 * @param mixed $value Site transient value.
1679 * @param int $expiration Time until expiration in seconds.
1681 do_action( 'setted_site_transient', $transient, $value, $expiration );