10 * Retrieve option value based on name of option.
12 * If the option does not exist or does not have a value, then the return value
13 * will be false. This is useful to check whether you need to install an option
14 * and is commonly used during installation of plugin options and to test
15 * whether upgrading is required.
17 * If the option was serialized then it will be unserialized when it is returned.
21 * @global wpdb $wpdb WordPress database abstraction object.
23 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
24 * @param mixed $default Optional. Default value to return if the option does not exist.
25 * @return mixed Value set for the option.
27 function get_option( $option, $default = false ) {
30 $option = trim( $option );
31 if ( empty( $option ) )
35 * Filter the value of an existing option before it is retrieved.
37 * The dynamic portion of the hook name, `$option`, refers to the option name.
39 * Passing a truthy value to the filter will short-circuit retrieving
40 * the option value, returning the passed value instead.
43 * @since 4.4.0 The `$option` parameter was added.
45 * @param bool|mixed $pre_option Value to return instead of the option value.
46 * Default false to skip it.
47 * @param string $option Option name.
49 $pre = apply_filters( 'pre_option_' . $option, false, $option );
53 if ( defined( 'WP_SETUP_CONFIG' ) )
56 if ( ! wp_installing() ) {
57 // prevent non-existent options from triggering multiple queries
58 $notoptions = wp_cache_get( 'notoptions', 'options' );
59 if ( isset( $notoptions[ $option ] ) ) {
61 * Filter the default value for an option.
63 * The dynamic portion of the hook name, `$option`, refers to the option name.
66 * @since 4.4.0 The `$option` parameter was added.
68 * @param mixed $default The default value to return if the option does not exist
70 * @param string $option Option name.
72 return apply_filters( 'default_option_' . $option, $default, $option );
75 $alloptions = wp_load_alloptions();
77 if ( isset( $alloptions[$option] ) ) {
78 $value = $alloptions[$option];
80 $value = wp_cache_get( $option, 'options' );
82 if ( false === $value ) {
83 $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
85 // Has to be get_row instead of get_var because of funkiness with 0, false, null values
86 if ( is_object( $row ) ) {
87 $value = $row->option_value;
88 wp_cache_add( $option, $value, 'options' );
89 } else { // option does not exist, so we must cache its non-existence
90 if ( ! is_array( $notoptions ) ) {
91 $notoptions = array();
93 $notoptions[$option] = true;
94 wp_cache_set( 'notoptions', $notoptions, 'options' );
96 /** This filter is documented in wp-includes/option.php */
97 return apply_filters( 'default_option_' . $option, $default, $option );
102 $suppress = $wpdb->suppress_errors();
103 $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
104 $wpdb->suppress_errors( $suppress );
105 if ( is_object( $row ) ) {
106 $value = $row->option_value;
108 /** This filter is documented in wp-includes/option.php */
109 return apply_filters( 'default_option_' . $option, $default, $option );
113 // If home is not set use siteurl.
114 if ( 'home' == $option && '' == $value )
115 return get_option( 'siteurl' );
117 if ( in_array( $option, array('siteurl', 'home', 'category_base', 'tag_base') ) )
118 $value = untrailingslashit( $value );
121 * Filter the value of an existing option.
123 * The dynamic portion of the hook name, `$option`, refers to the option name.
125 * @since 1.5.0 As 'option_' . $setting
127 * @since 4.4.0 The `$option` parameter was added.
129 * @param mixed $value Value of the option. If stored serialized, it will be
130 * unserialized prior to being returned.
131 * @param string $option Option name.
133 return apply_filters( 'option_' . $option, maybe_unserialize( $value ), $option );
137 * Protect WordPress special option from being modified.
139 * Will die if $option is in protected list. Protected options are 'alloptions'
140 * and 'notoptions' options.
144 * @param string $option Option name.
146 function wp_protect_special_option( $option ) {
147 if ( 'alloptions' === $option || 'notoptions' === $option )
148 wp_die( sprintf( __( '%s is a protected WP option and may not be modified' ), esc_html( $option ) ) );
152 * Print option value after sanitizing for forms.
156 * @param string $option Option name.
158 function form_option( $option ) {
159 echo esc_attr( get_option( $option ) );
163 * Loads and caches all autoloaded options, if available or all options.
167 * @global wpdb $wpdb WordPress database abstraction object.
169 * @return array List of all options.
171 function wp_load_alloptions() {
174 if ( ! wp_installing() || ! is_multisite() )
175 $alloptions = wp_cache_get( 'alloptions', 'options' );
179 if ( !$alloptions ) {
180 $suppress = $wpdb->suppress_errors();
181 if ( !$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) )
182 $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
183 $wpdb->suppress_errors($suppress);
184 $alloptions = array();
185 foreach ( (array) $alloptions_db as $o ) {
186 $alloptions[$o->option_name] = $o->option_value;
188 if ( ! wp_installing() || ! is_multisite() )
189 wp_cache_add( 'alloptions', $alloptions, 'options' );
196 * Loads and caches certain often requested site options if is_multisite() and a persistent cache is not being used.
200 * @global wpdb $wpdb WordPress database abstraction object.
202 * @param int $site_id Optional site ID for which to query the options. Defaults to the current site.
204 function wp_load_core_site_options( $site_id = null ) {
207 if ( ! is_multisite() || wp_using_ext_object_cache() || wp_installing() )
210 if ( empty($site_id) )
211 $site_id = $wpdb->siteid;
213 $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' );
215 $core_options_in = "'" . implode("', '", $core_options) . "'";
216 $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) );
218 foreach ( $options as $option ) {
219 $key = $option->meta_key;
220 $cache_key = "{$site_id}:$key";
221 $option->meta_value = maybe_unserialize( $option->meta_value );
223 wp_cache_set( $cache_key, $option->meta_value, 'site-options' );
228 * Update the value of an option that was already added.
230 * You do not need to serialize values. If the value needs to be serialized, then
231 * it will be serialized before it is inserted into the database. Remember,
232 * resources can not be serialized or added as an option.
234 * If the option does not exist, then the option will be added with the option value,
235 * with an `$autoload` value of 'yes'.
238 * @since 4.2.0 The `$autoload` parameter was added.
240 * @global wpdb $wpdb WordPress database abstraction object.
242 * @param string $option Option name. Expected to not be SQL-escaped.
243 * @param mixed $value Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
244 * @param string|bool $autoload Optional. Whether to load the option when WordPress starts up. For existing options,
245 * `$autoload` can only be updated using `update_option()` if `$value` is also changed.
246 * Accepts 'yes'|true to enable or 'no'|false to disable. For non-existent options,
247 * the default value is 'yes'. Default null.
248 * @return bool False if value was not updated and true if value was updated.
250 function update_option( $option, $value, $autoload = null ) {
253 $option = trim($option);
254 if ( empty($option) )
257 wp_protect_special_option( $option );
259 if ( is_object( $value ) )
260 $value = clone $value;
262 $value = sanitize_option( $option, $value );
263 $old_value = get_option( $option );
266 * Filter a specific option before its value is (maybe) serialized and updated.
268 * The dynamic portion of the hook name, `$option`, refers to the option name.
271 * @since 4.4.0 The `$option` parameter was added.
273 * @param mixed $value The new, unserialized option value.
274 * @param mixed $old_value The old option value.
275 * @param string $option Option name.
277 $value = apply_filters( 'pre_update_option_' . $option, $value, $old_value, $option );
280 * Filter an option before its value is (maybe) serialized and updated.
284 * @param mixed $value The new, unserialized option value.
285 * @param string $option Name of the option.
286 * @param mixed $old_value The old option value.
288 $value = apply_filters( 'pre_update_option', $value, $option, $old_value );
290 // If the new and old values are the same, no need to update.
291 if ( $value === $old_value )
294 /** This filter is documented in wp-includes/option.php */
295 if ( apply_filters( 'default_option_' . $option, false ) === $old_value ) {
296 // Default setting for new options is 'yes'.
297 if ( null === $autoload ) {
301 return add_option( $option, $value, '', $autoload );
304 $serialized_value = maybe_serialize( $value );
307 * Fires immediately before an option value is updated.
311 * @param string $option Name of the option to update.
312 * @param mixed $old_value The old option value.
313 * @param mixed $value The new option value.
315 do_action( 'update_option', $option, $old_value, $value );
317 $update_args = array(
318 'option_value' => $serialized_value,
321 if ( null !== $autoload ) {
322 $update_args['autoload'] = ( 'no' === $autoload || false === $autoload ) ? 'no' : 'yes';
325 $result = $wpdb->update( $wpdb->options, $update_args, array( 'option_name' => $option ) );
329 $notoptions = wp_cache_get( 'notoptions', 'options' );
330 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
331 unset( $notoptions[$option] );
332 wp_cache_set( 'notoptions', $notoptions, 'options' );
335 if ( ! wp_installing() ) {
336 $alloptions = wp_load_alloptions();
337 if ( isset( $alloptions[$option] ) ) {
338 $alloptions[ $option ] = $serialized_value;
339 wp_cache_set( 'alloptions', $alloptions, 'options' );
341 wp_cache_set( $option, $serialized_value, 'options' );
346 * Fires after the value of a specific option has been successfully updated.
348 * The dynamic portion of the hook name, `$option`, refers to the option name.
351 * @since 4.4.0 The `$option` parameter was added.
353 * @param mixed $old_value The old option value.
354 * @param mixed $value The new option value.
355 * @param string $option Option name.
357 do_action( "update_option_{$option}", $old_value, $value, $option );
360 * Fires after the value of an option has been successfully updated.
364 * @param string $option Name of the updated option.
365 * @param mixed $old_value The old option value.
366 * @param mixed $value The new option value.
368 do_action( 'updated_option', $option, $old_value, $value );
375 * You do not need to serialize values. If the value needs to be serialized, then
376 * it will be serialized before it is inserted into the database. Remember,
377 * resources can not be serialized or added as an option.
379 * You can create options without values and then update the values later.
380 * Existing options will not be updated and checks are performed to ensure that you
381 * aren't adding a protected WordPress option. Care should be taken to not name
382 * options the same as the ones which are protected.
386 * @global wpdb $wpdb WordPress database abstraction object.
388 * @param string $option Name of option to add. Expected to not be SQL-escaped.
389 * @param mixed $value Optional. Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
390 * @param string $deprecated Optional. Description. Not used anymore.
391 * @param string|bool $autoload Optional. Whether to load the option when WordPress starts up.
392 * Default is enabled. Accepts 'no' to disable for legacy reasons.
393 * @return bool False if option was not added and true if option was added.
395 function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) {
398 if ( !empty( $deprecated ) )
399 _deprecated_argument( __FUNCTION__, '2.3' );
401 $option = trim($option);
402 if ( empty($option) )
405 wp_protect_special_option( $option );
407 if ( is_object($value) )
408 $value = clone $value;
410 $value = sanitize_option( $option, $value );
412 // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
413 $notoptions = wp_cache_get( 'notoptions', 'options' );
414 if ( !is_array( $notoptions ) || !isset( $notoptions[$option] ) )
415 /** This filter is documented in wp-includes/option.php */
416 if ( apply_filters( 'default_option_' . $option, false ) !== get_option( $option ) )
419 $serialized_value = maybe_serialize( $value );
420 $autoload = ( 'no' === $autoload || false === $autoload ) ? 'no' : 'yes';
423 * Fires before an option is added.
427 * @param string $option Name of the option to add.
428 * @param mixed $value Value of the option.
430 do_action( 'add_option', $option, $value );
432 $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 ) );
436 if ( ! wp_installing() ) {
437 if ( 'yes' == $autoload ) {
438 $alloptions = wp_load_alloptions();
439 $alloptions[ $option ] = $serialized_value;
440 wp_cache_set( 'alloptions', $alloptions, 'options' );
442 wp_cache_set( $option, $serialized_value, 'options' );
446 // This option exists now
447 $notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh
448 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
449 unset( $notoptions[$option] );
450 wp_cache_set( 'notoptions', $notoptions, 'options' );
454 * Fires after a specific option has been added.
456 * The dynamic portion of the hook name, `$option`, refers to the option name.
458 * @since 2.5.0 As "add_option_{$name}"
461 * @param string $option Name of the option to add.
462 * @param mixed $value Value of the option.
464 do_action( "add_option_{$option}", $option, $value );
467 * Fires after an option has been added.
471 * @param string $option Name of the added option.
472 * @param mixed $value Value of the option.
474 do_action( 'added_option', $option, $value );
479 * Removes option by name. Prevents removal of protected WordPress options.
483 * @global wpdb $wpdb WordPress database abstraction object.
485 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
486 * @return bool True, if option is successfully deleted. False on failure.
488 function delete_option( $option ) {
491 $option = trim( $option );
492 if ( empty( $option ) )
495 wp_protect_special_option( $option );
497 // Get the ID, if no ID then return
498 $row = $wpdb->get_row( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) );
499 if ( is_null( $row ) )
503 * Fires immediately before an option is deleted.
507 * @param string $option Name of the option to delete.
509 do_action( 'delete_option', $option );
511 $result = $wpdb->delete( $wpdb->options, array( 'option_name' => $option ) );
512 if ( ! wp_installing() ) {
513 if ( 'yes' == $row->autoload ) {
514 $alloptions = wp_load_alloptions();
515 if ( is_array( $alloptions ) && isset( $alloptions[$option] ) ) {
516 unset( $alloptions[$option] );
517 wp_cache_set( 'alloptions', $alloptions, 'options' );
520 wp_cache_delete( $option, 'options' );
526 * Fires after a specific option has been deleted.
528 * The dynamic portion of the hook name, `$option`, refers to the option name.
532 * @param string $option Name of the deleted option.
534 do_action( "delete_option_$option", $option );
537 * Fires after an option has been deleted.
541 * @param string $option Name of the deleted option.
543 do_action( 'deleted_option', $option );
550 * Delete a transient.
554 * @param string $transient Transient name. Expected to not be SQL-escaped.
555 * @return bool true if successful, false otherwise
557 function delete_transient( $transient ) {
560 * Fires immediately before a specific transient is deleted.
562 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
566 * @param string $transient Transient name.
568 do_action( 'delete_transient_' . $transient, $transient );
570 if ( wp_using_ext_object_cache() ) {
571 $result = wp_cache_delete( $transient, 'transient' );
573 $option_timeout = '_transient_timeout_' . $transient;
574 $option = '_transient_' . $transient;
575 $result = delete_option( $option );
577 delete_option( $option_timeout );
583 * Fires after a transient is deleted.
587 * @param string $transient Deleted transient name.
589 do_action( 'deleted_transient', $transient );
596 * Get the value of a transient.
598 * If the transient does not exist, does not have a value, or has expired,
599 * then the return value will be false.
603 * @param string $transient Transient name. Expected to not be SQL-escaped.
604 * @return mixed Value of transient.
606 function get_transient( $transient ) {
609 * Filter the value of an existing transient.
611 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
613 * Passing a truthy value to the filter will effectively short-circuit retrieval
614 * of the transient, returning the passed value instead.
617 * @since 4.4.0 The `$transient` parameter was added
619 * @param mixed $pre_transient The default value to return if the transient does not exist.
620 * Any value other than false will short-circuit the retrieval
621 * of the transient, and return the returned value.
622 * @param string $transient Transient name.
624 $pre = apply_filters( 'pre_transient_' . $transient, false, $transient );
625 if ( false !== $pre )
628 if ( wp_using_ext_object_cache() ) {
629 $value = wp_cache_get( $transient, 'transient' );
631 $transient_option = '_transient_' . $transient;
632 if ( ! wp_installing() ) {
633 // If option is not in alloptions, it is not autoloaded and thus has a timeout
634 $alloptions = wp_load_alloptions();
635 if ( !isset( $alloptions[$transient_option] ) ) {
636 $transient_timeout = '_transient_timeout_' . $transient;
637 $timeout = get_option( $transient_timeout );
638 if ( false !== $timeout && $timeout < time() ) {
639 delete_option( $transient_option );
640 delete_option( $transient_timeout );
646 if ( ! isset( $value ) )
647 $value = get_option( $transient_option );
651 * Filter an existing transient's value.
653 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
656 * @since 4.4.0 The `$transient` parameter was added
658 * @param mixed $value Value of transient.
659 * @param string $transient Transient name.
661 return apply_filters( 'transient_' . $transient, $value, $transient );
665 * Set/update the value of a transient.
667 * You do not need to serialize values. If the value needs to be serialized, then
668 * it will be serialized before it is set.
672 * @param string $transient Transient name. Expected to not be SQL-escaped. Must be
673 * 172 characters or fewer in length.
674 * @param mixed $value Transient value. Must be serializable if non-scalar.
675 * Expected to not be SQL-escaped.
676 * @param int $expiration Optional. Time until expiration in seconds. Default 0 (no expiration).
677 * @return bool False if value was not set and true if value was set.
679 function set_transient( $transient, $value, $expiration = 0 ) {
681 $expiration = (int) $expiration;
684 * Filter a specific transient before its value is set.
686 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
689 * @since 4.2.0 The `$expiration` parameter was added.
690 * @since 4.4.0 The `$transient` parameter was added.
692 * @param mixed $value New value of transient.
693 * @param int $expiration Time until expiration in seconds.
694 * @param string $transient Transient name.
696 $value = apply_filters( 'pre_set_transient_' . $transient, $value, $expiration, $transient );
699 * Filter the expiration for a transient before its value is set.
701 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
705 * @param int $expiration Time until expiration in seconds. Use 0 for no expiration.
706 * @param mixed $value New value of transient.
707 * @param string $transient Transient name.
709 $expiration = apply_filters( 'expiration_of_transient_' . $transient, $expiration, $value, $transient );
711 if ( wp_using_ext_object_cache() ) {
712 $result = wp_cache_set( $transient, $value, 'transient', $expiration );
714 $transient_timeout = '_transient_timeout_' . $transient;
715 $transient_option = '_transient_' . $transient;
716 if ( false === get_option( $transient_option ) ) {
720 add_option( $transient_timeout, time() + $expiration, '', 'no' );
722 $result = add_option( $transient_option, $value, '', $autoload );
724 // If expiration is requested, but the transient has no timeout option,
725 // delete, then re-create transient rather than update.
728 if ( false === get_option( $transient_timeout ) ) {
729 delete_option( $transient_option );
730 add_option( $transient_timeout, time() + $expiration, '', 'no' );
731 $result = add_option( $transient_option, $value, '', 'no' );
734 update_option( $transient_timeout, time() + $expiration );
738 $result = update_option( $transient_option, $value );
746 * Fires after the value for a specific transient has been set.
748 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
751 * @since 3.6.0 The `$value` and `$expiration` parameters were added.
752 * @since 4.4.0 The `$transient` parameter was added.
754 * @param mixed $value Transient value.
755 * @param int $expiration Time until expiration in seconds.
756 * @param string $transient The name of the transient.
758 do_action( 'set_transient_' . $transient, $value, $expiration, $transient );
761 * Fires after the value for a transient has been set.
764 * @since 3.6.0 The `$value` and `$expiration` parameters were added.
766 * @param string $transient The name of the transient.
767 * @param mixed $value Transient value.
768 * @param int $expiration Time until expiration in seconds.
770 do_action( 'setted_transient', $transient, $value, $expiration );
776 * Saves and restores user interface settings stored in a cookie.
778 * Checks if the current user-settings cookie is updated and stores it. When no
779 * cookie exists (different browser used), adds the last saved cookie restoring
784 function wp_user_settings() {
786 if ( ! is_admin() || defined( 'DOING_AJAX' ) ) {
790 if ( ! $user_id = get_current_user_id() ) {
794 if ( is_super_admin() && ! is_user_member_of_blog() ) {
798 $settings = (string) get_user_option( 'user-settings', $user_id );
800 if ( isset( $_COOKIE['wp-settings-' . $user_id] ) ) {
801 $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user_id] );
803 // No change or both empty
804 if ( $cookie == $settings )
807 $last_saved = (int) get_user_option( 'user-settings-time', $user_id );
808 $current = isset( $_COOKIE['wp-settings-time-' . $user_id]) ? preg_replace( '/[^0-9]/', '', $_COOKIE['wp-settings-time-' . $user_id] ) : 0;
810 // The cookie is newer than the saved value. Update the user_option and leave the cookie as-is
811 if ( $current > $last_saved ) {
812 update_user_option( $user_id, 'user-settings', $cookie, false );
813 update_user_option( $user_id, 'user-settings-time', time() - 5, false );
818 // The cookie is not set in the current browser or the saved value is newer.
819 $secure = ( 'https' === parse_url( admin_url(), PHP_URL_SCHEME ) );
820 setcookie( 'wp-settings-' . $user_id, $settings, time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure );
821 setcookie( 'wp-settings-time-' . $user_id, time(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure );
822 $_COOKIE['wp-settings-' . $user_id] = $settings;
826 * Retrieve user interface setting value based on setting name.
830 * @param string $name The name of the setting.
831 * @param string $default Optional default value to return when $name is not set.
832 * @return mixed the last saved user setting or the default value/false if it doesn't exist.
834 function get_user_setting( $name, $default = false ) {
835 $all_user_settings = get_all_user_settings();
837 return isset( $all_user_settings[$name] ) ? $all_user_settings[$name] : $default;
841 * Add or update user interface setting.
843 * Both $name and $value can contain only ASCII letters, numbers and underscores.
844 * This function has to be used before any output has started as it calls setcookie().
848 * @param string $name The name of the setting.
849 * @param string $value The value for the setting.
850 * @return bool|void true if set successfully/false if not.
852 function set_user_setting( $name, $value ) {
853 if ( headers_sent() ) {
857 $all_user_settings = get_all_user_settings();
858 $all_user_settings[$name] = $value;
860 return wp_set_all_user_settings( $all_user_settings );
864 * Delete user interface settings.
866 * Deleting settings would reset them to the defaults.
867 * This function has to be used before any output has started as it calls setcookie().
871 * @param string $names The name or array of names of the setting to be deleted.
872 * @return bool|void true if deleted successfully/false if not.
874 function delete_user_setting( $names ) {
875 if ( headers_sent() ) {
879 $all_user_settings = get_all_user_settings();
880 $names = (array) $names;
883 foreach ( $names as $name ) {
884 if ( isset( $all_user_settings[$name] ) ) {
885 unset( $all_user_settings[$name] );
891 return wp_set_all_user_settings( $all_user_settings );
898 * Retrieve all user interface settings.
902 * @global array $_updated_user_settings
904 * @return array the last saved user settings or empty array.
906 function get_all_user_settings() {
907 global $_updated_user_settings;
909 if ( ! $user_id = get_current_user_id() ) {
913 if ( isset( $_updated_user_settings ) && is_array( $_updated_user_settings ) ) {
914 return $_updated_user_settings;
917 $user_settings = array();
919 if ( isset( $_COOKIE['wp-settings-' . $user_id] ) ) {
920 $cookie = preg_replace( '/[^A-Za-z0-9=&_-]/', '', $_COOKIE['wp-settings-' . $user_id] );
922 if ( strpos( $cookie, '=' ) ) { // '=' cannot be 1st char
923 parse_str( $cookie, $user_settings );
926 $option = get_user_option( 'user-settings', $user_id );
928 if ( $option && is_string( $option ) ) {
929 parse_str( $option, $user_settings );
933 $_updated_user_settings = $user_settings;
934 return $user_settings;
938 * Private. Set all user interface settings.
942 * @global array $_updated_user_settings
944 * @param array $user_settings
947 function wp_set_all_user_settings( $user_settings ) {
948 global $_updated_user_settings;
950 if ( ! $user_id = get_current_user_id() ) {
954 if ( is_super_admin() && ! is_user_member_of_blog() ) {
959 foreach ( $user_settings as $name => $value ) {
960 $_name = preg_replace( '/[^A-Za-z0-9_-]+/', '', $name );
961 $_value = preg_replace( '/[^A-Za-z0-9_-]+/', '', $value );
963 if ( ! empty( $_name ) ) {
964 $settings .= $_name . '=' . $_value . '&';
968 $settings = rtrim( $settings, '&' );
969 parse_str( $settings, $_updated_user_settings );
971 update_user_option( $user_id, 'user-settings', $settings, false );
972 update_user_option( $user_id, 'user-settings-time', time(), false );
978 * Delete the user settings of the current user.
982 function delete_all_user_settings() {
983 if ( ! $user_id = get_current_user_id() ) {
987 update_user_option( $user_id, 'user-settings', '', false );
988 setcookie( 'wp-settings-' . $user_id, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH );
992 * Retrieve an option value for the current network based on name of option.
995 * @since 4.4.0 The `$use_cache` parameter was deprecated.
996 * @since 4.4.0 Modified into wrapper for get_network_option()
998 * @see get_network_option()
1000 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
1001 * @param mixed $default Optional value to return if option doesn't exist. Default false.
1002 * @param bool $deprecated Whether to use cache. Multisite only. Always set to true.
1003 * @return mixed Value set for the option.
1005 function get_site_option( $option, $default = false, $deprecated = true ) {
1006 return get_network_option( null, $option, $default );
1010 * Add a new option for the current network.
1012 * Existing options will not be updated. Note that prior to 3.3 this wasn't the case.
1015 * @since 4.4.0 Modified into wrapper for add_network_option()
1017 * @see add_network_option()
1019 * @param string $option Name of option to add. Expected to not be SQL-escaped.
1020 * @param mixed $value Option value, can be anything. Expected to not be SQL-escaped.
1021 * @return bool False if the option was not added. True if the option was added.
1023 function add_site_option( $option, $value ) {
1024 return add_network_option( null, $option, $value );
1028 * Removes a option by name for the current network.
1031 * @since 4.4.0 Modified into wrapper for delete_network_option()
1033 * @see delete_network_option()
1035 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
1036 * @return bool True, if succeed. False, if failure.
1038 function delete_site_option( $option ) {
1039 return delete_network_option( null, $option );
1043 * Update the value of an option that was already added for the current network.
1046 * @since 4.4.0 Modified into wrapper for update_network_option()
1048 * @see update_network_option()
1050 * @param string $option Name of option. Expected to not be SQL-escaped.
1051 * @param mixed $value Option value. Expected to not be SQL-escaped.
1052 * @return bool False if value was not updated. True if value was updated.
1054 function update_site_option( $option, $value ) {
1055 return update_network_option( null, $option, $value );
1059 * Retrieve a network's option value based on the option name.
1065 * @global wpdb $wpdb
1066 * @global object $current_site
1068 * @param int $network_id ID of the network. Can be null to default to the current network ID.
1069 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
1070 * @param mixed $default Optional. Value to return if the option doesn't exist. Default false.
1071 * @return mixed Value set for the option.
1073 function get_network_option( $network_id, $option, $default = false ) {
1074 global $wpdb, $current_site;
1076 if ( $network_id && ! is_numeric( $network_id ) ) {
1080 $network_id = (int) $network_id;
1082 // Fallback to the current network if a network ID is not specified.
1083 if ( ! $network_id && is_multisite() ) {
1084 $network_id = $current_site->id;
1088 * Filter an existing network option before it is retrieved.
1090 * The dynamic portion of the hook name, `$option`, refers to the option name.
1092 * Passing a truthy value to the filter will effectively short-circuit retrieval,
1093 * returning the passed value instead.
1095 * @since 2.9.0 As 'pre_site_option_' . $key
1097 * @since 4.4.0 The `$option` parameter was added
1099 * @param mixed $pre_option The default value to return if the option does not exist.
1100 * @param string $option Option name.
1102 $pre = apply_filters( 'pre_site_option_' . $option, false, $option );
1104 if ( false !== $pre ) {
1108 // prevent non-existent options from triggering multiple queries
1109 $notoptions_key = "$network_id:notoptions";
1110 $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
1112 if ( isset( $notoptions[ $option ] ) ) {
1115 * Filter a specific default network option.
1117 * The dynamic portion of the hook name, `$option`, refers to the option name.
1120 * @since 4.4.0 The `$option` parameter was added.
1122 * @param mixed $default The value to return if the site option does not exist
1124 * @param string $option Option name.
1126 return apply_filters( 'default_site_option_' . $option, $default, $option );
1129 if ( ! is_multisite() ) {
1130 /** This filter is documented in wp-includes/option.php */
1131 $default = apply_filters( 'default_site_option_' . $option, $default, $option );
1132 $value = get_option( $option, $default );
1134 $cache_key = "$network_id:$option";
1135 $value = wp_cache_get( $cache_key, 'site-options' );
1137 if ( ! isset( $value ) || false === $value ) {
1138 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $network_id ) );
1140 // Has to be get_row instead of get_var because of funkiness with 0, false, null values
1141 if ( is_object( $row ) ) {
1142 $value = $row->meta_value;
1143 $value = maybe_unserialize( $value );
1144 wp_cache_set( $cache_key, $value, 'site-options' );
1146 if ( ! is_array( $notoptions ) ) {
1147 $notoptions = array();
1149 $notoptions[ $option ] = true;
1150 wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
1152 /** This filter is documented in wp-includes/option.php */
1153 $value = apply_filters( 'default_site_option_' . $option, $default, $option );
1159 * Filter the value of an existing network option.
1161 * The dynamic portion of the hook name, `$option`, refers to the option name.
1163 * @since 2.9.0 As 'site_option_' . $key
1165 * @since 4.4.0 The `$option` parameter was added
1167 * @param mixed $value Value of network option.
1168 * @param string $option Option name.
1170 return apply_filters( 'site_option_' . $option, $value, $option );
1174 * Add a new network option.
1176 * Existing options will not be updated.
1182 * @global wpdb $wpdb
1183 * @global object $current_site
1185 * @param int $network_id ID of the network. Can be null to default to the current network ID.
1186 * @param string $option Name of option to add. Expected to not be SQL-escaped.
1187 * @param mixed $value Option value, can be anything. Expected to not be SQL-escaped.
1188 * @return bool False if option was not added and true if option was added.
1190 function add_network_option( $network_id, $option, $value ) {
1191 global $wpdb, $current_site;
1193 if ( $network_id && ! is_numeric( $network_id ) ) {
1197 $network_id = (int) $network_id;
1199 // Fallback to the current network if a network ID is not specified.
1200 if ( ! $network_id && is_multisite() ) {
1201 $network_id = $current_site->id;
1204 wp_protect_special_option( $option );
1207 * Filter the value of a specific network option before it is added.
1209 * The dynamic portion of the hook name, `$option`, refers to the option name.
1211 * @since 2.9.0 As 'pre_add_site_option_' . $key
1213 * @since 4.4.0 The `$option` parameter was added
1215 * @param mixed $value Value of network option.
1216 * @param string $option Option name.
1218 $value = apply_filters( 'pre_add_site_option_' . $option, $value, $option );
1220 $notoptions_key = "$network_id:notoptions";
1222 if ( ! is_multisite() ) {
1223 $result = add_option( $option, $value );
1225 $cache_key = "$network_id:$option";
1227 // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
1228 $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
1229 if ( ! is_array( $notoptions ) || ! isset( $notoptions[ $option ] ) ) {
1230 if ( false !== get_network_option( $network_id, $option, false ) ) {
1235 $value = sanitize_option( $option, $value );
1237 $serialized_value = maybe_serialize( $value );
1238 $result = $wpdb->insert( $wpdb->sitemeta, array( 'site_id' => $network_id, 'meta_key' => $option, 'meta_value' => $serialized_value ) );
1244 wp_cache_set( $cache_key, $value, 'site-options' );
1246 // This option exists now
1247 $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); // yes, again... we need it to be fresh
1248 if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
1249 unset( $notoptions[ $option ] );
1250 wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
1257 * Fires after a specific network option has been successfully added.
1259 * The dynamic portion of the hook name, `$option`, refers to the option name.
1261 * @since 2.9.0 As "add_site_option_{$key}"
1264 * @param string $option Name of the network option.
1265 * @param mixed $value Value of the network option.
1267 do_action( 'add_site_option_' . $option, $option, $value );
1270 * Fires after a network option has been successfully added.
1274 * @param string $option Name of the network option.
1275 * @param mixed $value Value of the network option.
1277 do_action( 'add_site_option', $option, $value );
1286 * Removes a network option by name.
1290 * @see delete_option()
1292 * @global wpdb $wpdb
1293 * @global object $current_site
1295 * @param int $network_id ID of the network. Can be null to default to the current network ID.
1296 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
1297 * @return bool True, if succeed. False, if failure.
1299 function delete_network_option( $network_id, $option ) {
1300 global $wpdb, $current_site;
1302 if ( $network_id && ! is_numeric( $network_id ) ) {
1306 $network_id = (int) $network_id;
1308 // Fallback to the current network if a network ID is not specified.
1309 if ( ! $network_id && is_multisite() ) {
1310 $network_id = $current_site->id;
1314 * Fires immediately before a specific network option is deleted.
1316 * The dynamic portion of the hook name, `$option`, refers to the option name.
1319 * @since 4.4.0 The `$option` parameter was added
1321 * @param string $option Option name.
1323 do_action( 'pre_delete_site_option_' . $option, $option );
1325 if ( ! is_multisite() ) {
1326 $result = delete_option( $option );
1328 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $network_id ) );
1329 if ( is_null( $row ) || ! $row->meta_id ) {
1332 $cache_key = "$network_id:$option";
1333 wp_cache_delete( $cache_key, 'site-options' );
1335 $result = $wpdb->delete( $wpdb->sitemeta, array( 'meta_key' => $option, 'site_id' => $network_id ) );
1341 * Fires after a specific network option has been deleted.
1343 * The dynamic portion of the hook name, `$option`, refers to the option name.
1345 * @since 2.9.0 As "delete_site_option_{$key}"
1348 * @param string $option Name of the network option.
1350 do_action( 'delete_site_option_' . $option, $option );
1353 * Fires after a network option has been deleted.
1357 * @param string $option Name of the network option.
1359 do_action( 'delete_site_option', $option );
1368 * Update the value of a network option that was already added.
1372 * @see update_option()
1374 * @global wpdb $wpdb
1375 * @global object $current_site
1377 * @param int $network_id ID of the network. Can be null to default to the current network ID.
1378 * @param string $option Name of option. Expected to not be SQL-escaped.
1379 * @param mixed $value Option value. Expected to not be SQL-escaped.
1380 * @return bool False if value was not updated and true if value was updated.
1382 function update_network_option( $network_id, $option, $value ) {
1383 global $wpdb, $current_site;
1385 if ( $network_id && ! is_numeric( $network_id ) ) {
1389 $network_id = (int) $network_id;
1391 // Fallback to the current network if a network ID is not specified.
1392 if ( ! $network_id && is_multisite() ) {
1393 $network_id = $current_site->id;
1396 wp_protect_special_option( $option );
1398 $old_value = get_network_option( $network_id, $option, false );
1401 * Filter a specific network option before its value is updated.
1403 * The dynamic portion of the hook name, `$option`, refers to the option name.
1405 * @since 2.9.0 As 'pre_update_site_option_' . $key
1407 * @since 4.4.0 The `$option` parameter was added
1409 * @param mixed $value New value of the network option.
1410 * @param mixed $old_value Old value of the network option.
1411 * @param string $option Option name.
1413 $value = apply_filters( 'pre_update_site_option_' . $option, $value, $old_value, $option );
1415 if ( $value === $old_value ) {
1419 if ( false === $old_value ) {
1420 return add_network_option( $network_id, $option, $value );
1423 $notoptions_key = "$network_id:notoptions";
1424 $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
1425 if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
1426 unset( $notoptions[ $option ] );
1427 wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
1430 if ( ! is_multisite() ) {
1431 $result = update_option( $option, $value );
1433 $value = sanitize_option( $option, $value );
1435 $serialized_value = maybe_serialize( $value );
1436 $result = $wpdb->update( $wpdb->sitemeta, array( 'meta_value' => $serialized_value ), array( 'site_id' => $network_id, 'meta_key' => $option ) );
1439 $cache_key = "$network_id:$option";
1440 wp_cache_set( $cache_key, $value, 'site-options' );
1447 * Fires after the value of a specific network option has been successfully updated.
1449 * The dynamic portion of the hook name, `$option`, refers to the option name.
1451 * @since 2.9.0 As "update_site_option_{$key}"
1454 * @param string $option Name of the network option.
1455 * @param mixed $value Current value of the network option.
1456 * @param mixed $old_value Old value of the network option.
1458 do_action( 'update_site_option_' . $option, $option, $value, $old_value );
1461 * Fires after the value of a network option has been successfully updated.
1465 * @param string $option Name of the network option.
1466 * @param mixed $value Current value of the network option.
1467 * @param mixed $old_value Old value of the network option.
1469 do_action( 'update_site_option', $option, $value, $old_value );
1478 * Delete a site transient.
1482 * @param string $transient Transient name. Expected to not be SQL-escaped.
1483 * @return bool True if successful, false otherwise
1485 function delete_site_transient( $transient ) {
1488 * Fires immediately before a specific site transient is deleted.
1490 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1494 * @param string $transient Transient name.
1496 do_action( 'delete_site_transient_' . $transient, $transient );
1498 if ( wp_using_ext_object_cache() ) {
1499 $result = wp_cache_delete( $transient, 'site-transient' );
1501 $option_timeout = '_site_transient_timeout_' . $transient;
1502 $option = '_site_transient_' . $transient;
1503 $result = delete_site_option( $option );
1505 delete_site_option( $option_timeout );
1510 * Fires after a transient is deleted.
1514 * @param string $transient Deleted transient name.
1516 do_action( 'deleted_site_transient', $transient );
1523 * Get the value of a site transient.
1525 * If the transient does not exist, does not have a value, or has expired,
1526 * then the return value will be false.
1530 * @see get_transient()
1532 * @param string $transient Transient name. Expected to not be SQL-escaped.
1533 * @return mixed Value of transient.
1535 function get_site_transient( $transient ) {
1538 * Filter the value of an existing site transient.
1540 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1542 * Passing a truthy value to the filter will effectively short-circuit retrieval,
1543 * returning the passed value instead.
1546 * @since 4.4.0 The `$transient` parameter was added.
1548 * @param mixed $pre_site_transient The default value to return if the site transient does not exist.
1549 * Any value other than false will short-circuit the retrieval
1550 * of the transient, and return the returned value.
1551 * @param string $transient Transient name.
1553 $pre = apply_filters( 'pre_site_transient_' . $transient, false, $transient );
1555 if ( false !== $pre )
1558 if ( wp_using_ext_object_cache() ) {
1559 $value = wp_cache_get( $transient, 'site-transient' );
1561 // Core transients that do not have a timeout. Listed here so querying timeouts can be avoided.
1562 $no_timeout = array('update_core', 'update_plugins', 'update_themes');
1563 $transient_option = '_site_transient_' . $transient;
1564 if ( ! in_array( $transient, $no_timeout ) ) {
1565 $transient_timeout = '_site_transient_timeout_' . $transient;
1566 $timeout = get_site_option( $transient_timeout );
1567 if ( false !== $timeout && $timeout < time() ) {
1568 delete_site_option( $transient_option );
1569 delete_site_option( $transient_timeout );
1574 if ( ! isset( $value ) )
1575 $value = get_site_option( $transient_option );
1579 * Filter the value of an existing site transient.
1581 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1584 * @since 4.4.0 The `$transient` parameter was added.
1586 * @param mixed $value Value of site transient.
1587 * @param string $transient Transient name.
1589 return apply_filters( 'site_transient_' . $transient, $value, $transient );
1593 * Set/update the value of a site transient.
1595 * You do not need to serialize values, if the value needs to be serialize, then
1596 * it will be serialized before it is set.
1600 * @see set_transient()
1602 * @param string $transient Transient name. Expected to not be SQL-escaped. Must be
1603 * 40 characters or fewer in length.
1604 * @param mixed $value Transient value. Expected to not be SQL-escaped.
1605 * @param int $expiration Optional. Time until expiration in seconds. Default 0 (no expiration).
1606 * @return bool False if value was not set and true if value was set.
1608 function set_site_transient( $transient, $value, $expiration = 0 ) {
1611 * Filter the value of a specific site transient before it is set.
1613 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1616 * @since 4.4.0 The `$transient` parameter was added.
1618 * @param mixed $value New value of site transient.
1619 * @param string $transient Transient name.
1621 $value = apply_filters( 'pre_set_site_transient_' . $transient, $value, $transient );
1623 $expiration = (int) $expiration;
1626 * Filter the expiration for a site transient before its value is set.
1628 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1632 * @param int $expiration Time until expiration in seconds. Use 0 for no expiration.
1633 * @param mixed $value New value of site transient.
1634 * @param string $transient Transient name.
1636 $expiration = apply_filters( 'expiration_of_site_transient_' . $transient, $expiration, $value, $transient );
1638 if ( wp_using_ext_object_cache() ) {
1639 $result = wp_cache_set( $transient, $value, 'site-transient', $expiration );
1641 $transient_timeout = '_site_transient_timeout_' . $transient;
1642 $option = '_site_transient_' . $transient;
1643 if ( false === get_site_option( $option ) ) {
1645 add_site_option( $transient_timeout, time() + $expiration );
1646 $result = add_site_option( $option, $value );
1649 update_site_option( $transient_timeout, time() + $expiration );
1650 $result = update_site_option( $option, $value );
1656 * Fires after the value for a specific site transient has been set.
1658 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1661 * @since 4.4.0 The `$transient` parameter was added
1663 * @param mixed $value Site transient value.
1664 * @param int $expiration Time until expiration in seconds.
1665 * @param string $transient Transient name.
1667 do_action( 'set_site_transient_' . $transient, $value, $expiration, $transient );
1670 * Fires after the value for a site transient has been set.
1674 * @param string $transient The name of the site transient.
1675 * @param mixed $value Site transient value.
1676 * @param int $expiration Time until expiration in seconds.
1678 do_action( 'setted_site_transient', $transient, $value, $expiration );