10 * Retrieve option value based on name of option.
12 * If the option does not exist or does not have a value, then the return value
13 * will be false. This is useful to check whether you need to install an option
14 * and is commonly used during installation of plugin options and to test
15 * whether upgrading is required.
17 * If the option was serialized then it will be unserialized when it is returned.
21 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
22 * @param mixed $default Optional. Default value to return if the option does not exist.
23 * @return mixed Value set for the option.
25 function get_option( $option, $default = false ) {
28 $option = trim( $option );
29 if ( empty( $option ) )
33 * Filter the value of an existing option before it is retrieved.
35 * The dynamic portion of the hook name, $option, refers to the option name.
37 * Passing a truthy value to the filter will short-circuit retrieving
38 * the option value, returning the passed value instead.
42 * @param bool|mixed $pre_option Value to return instead of the option value.
43 * Default false to skip it.
45 $pre = apply_filters( 'pre_option_' . $option, false );
49 if ( defined( 'WP_SETUP_CONFIG' ) )
52 if ( ! defined( 'WP_INSTALLING' ) ) {
53 // prevent non-existent options from triggering multiple queries
54 $notoptions = wp_cache_get( 'notoptions', 'options' );
55 if ( isset( $notoptions[$option] ) )
58 * Filter the default value for an option.
60 * The dynamic portion of the hook name, $option, refers
65 * @param mixed $default The default value to return if the option
66 * does not exist in the database.
68 return apply_filters( 'default_option_' . $option, $default );
70 $alloptions = wp_load_alloptions();
72 if ( isset( $alloptions[$option] ) ) {
73 $value = $alloptions[$option];
75 $value = wp_cache_get( $option, 'options' );
77 if ( false === $value ) {
78 $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
80 // Has to be get_row instead of get_var because of funkiness with 0, false, null values
81 if ( is_object( $row ) ) {
82 $value = $row->option_value;
83 wp_cache_add( $option, $value, 'options' );
84 } else { // option does not exist, so we must cache its non-existence
85 $notoptions[$option] = true;
86 wp_cache_set( 'notoptions', $notoptions, 'options' );
88 /** This filter is documented in wp-includes/option.php */
89 return apply_filters( 'default_option_' . $option, $default );
94 $suppress = $wpdb->suppress_errors();
95 $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
96 $wpdb->suppress_errors( $suppress );
97 if ( is_object( $row ) ) {
98 $value = $row->option_value;
100 /** This filter is documented in wp-includes/option.php */
101 return apply_filters( 'default_option_' . $option, $default );
105 // If home is not set use siteurl.
106 if ( 'home' == $option && '' == $value )
107 return get_option( 'siteurl' );
109 if ( in_array( $option, array('siteurl', 'home', 'category_base', 'tag_base') ) )
110 $value = untrailingslashit( $value );
113 * Filter the value of an existing option.
115 * The dynamic portion of the hook name, $option, refers to the option name.
117 * @since 1.5.0 As 'option_' . $setting
120 * @param mixed $value Value of the option. If stored serialized, it will be
121 * unserialized prior to being returned.
123 return apply_filters( 'option_' . $option, maybe_unserialize( $value ) );
127 * Protect WordPress special option from being modified.
129 * Will die if $option is in protected list. Protected options are 'alloptions'
130 * and 'notoptions' options.
134 * @param string $option Option name.
136 function wp_protect_special_option( $option ) {
137 if ( 'alloptions' === $option || 'notoptions' === $option )
138 wp_die( sprintf( __( '%s is a protected WP option and may not be modified' ), esc_html( $option ) ) );
142 * Print option value after sanitizing for forms.
144 * @uses attr Sanitizes value.
147 * @param string $option Option name.
149 function form_option( $option ) {
150 echo esc_attr( get_option( $option ) );
154 * Loads and caches all autoloaded options, if available or all options.
158 * @return array List of all options.
160 function wp_load_alloptions() {
163 if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
164 $alloptions = wp_cache_get( 'alloptions', 'options' );
168 if ( !$alloptions ) {
169 $suppress = $wpdb->suppress_errors();
170 if ( !$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) )
171 $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
172 $wpdb->suppress_errors($suppress);
173 $alloptions = array();
174 foreach ( (array) $alloptions_db as $o ) {
175 $alloptions[$o->option_name] = $o->option_value;
177 if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
178 wp_cache_add( 'alloptions', $alloptions, 'options' );
185 * Loads and caches certain often requested site options if is_multisite() and a persistent cache is not being used.
189 * @param int $site_id Optional site ID for which to query the options. Defaults to the current site.
191 function wp_load_core_site_options( $site_id = null ) {
194 if ( !is_multisite() || wp_using_ext_object_cache() || defined( 'WP_INSTALLING' ) )
197 if ( empty($site_id) )
198 $site_id = $wpdb->siteid;
200 $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' );
202 $core_options_in = "'" . implode("', '", $core_options) . "'";
203 $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) );
205 foreach ( $options as $option ) {
206 $key = $option->meta_key;
207 $cache_key = "{$site_id}:$key";
208 $option->meta_value = maybe_unserialize( $option->meta_value );
210 wp_cache_set( $cache_key, $option->meta_value, 'site-options' );
215 * Update the value of an option that was already added.
217 * You do not need to serialize values. If the value needs to be serialized, then
218 * it will be serialized before it is inserted into the database. Remember,
219 * resources can not be serialized or added as an option.
221 * If the option does not exist, then the option will be added with the option
222 * value, but you will not be able to set whether it is autoloaded. If you want
223 * to set whether an option is autoloaded, then you need to use the add_option().
227 * @param string $option Option name. Expected to not be SQL-escaped.
228 * @param mixed $value Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
229 * @return bool False if value was not updated and true if value was updated.
231 function update_option( $option, $value ) {
234 $option = trim($option);
235 if ( empty($option) )
238 wp_protect_special_option( $option );
240 if ( is_object( $value ) )
241 $value = clone $value;
243 $value = sanitize_option( $option, $value );
244 $old_value = get_option( $option );
247 * Filter a specific option before its value is (maybe) serialized and updated.
249 * The dynamic portion of the hook name, $option, refers to the option name.
253 * @param mixed $value The new, unserialized option value.
254 * @param mixed $old_value The old option value.
256 $value = apply_filters( 'pre_update_option_' . $option, $value, $old_value );
259 * Filter an option before its value is (maybe) serialized and updated.
263 * @param mixed $value The new, unserialized option value.
264 * @param string $option Name of the option.
265 * @param mixed $old_value The old option value.
267 $value = apply_filters( 'pre_update_option', $value, $option, $old_value );
269 // If the new and old values are the same, no need to update.
270 if ( $value === $old_value )
273 if ( false === $old_value )
274 return add_option( $option, $value );
276 $serialized_value = maybe_serialize( $value );
279 * Fires immediately before an option value is updated.
283 * @param string $option Name of the option to update.
284 * @param mixed $old_value The old option value.
285 * @param mixed $value The new option value.
287 do_action( 'update_option', $option, $old_value, $value );
289 $result = $wpdb->update( $wpdb->options, array( 'option_value' => $serialized_value ), array( 'option_name' => $option ) );
293 $notoptions = wp_cache_get( 'notoptions', 'options' );
294 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
295 unset( $notoptions[$option] );
296 wp_cache_set( 'notoptions', $notoptions, 'options' );
299 if ( ! defined( 'WP_INSTALLING' ) ) {
300 $alloptions = wp_load_alloptions();
301 if ( isset( $alloptions[$option] ) ) {
302 $alloptions[ $option ] = $serialized_value;
303 wp_cache_set( 'alloptions', $alloptions, 'options' );
305 wp_cache_set( $option, $serialized_value, 'options' );
310 * Fires after the value of a specific option has been successfully updated.
312 * The dynamic portion of the hook name, $option, refers to the option name.
316 * @param mixed $old_value The old option value.
317 * @param mixed $value The new option value.
319 do_action( "update_option_{$option}", $old_value, $value );
322 * Fires after the value of an option has been successfully updated.
326 * @param string $option Name of the updated option.
327 * @param mixed $old_value The old option value.
328 * @param mixed $value The new option value.
330 do_action( 'updated_option', $option, $old_value, $value );
337 * You do not need to serialize values. If the value needs to be serialized, then
338 * it will be serialized before it is inserted into the database. Remember,
339 * resources can not be serialized or added as an option.
341 * You can create options without values and then update the values later.
342 * Existing options will not be updated and checks are performed to ensure that you
343 * aren't adding a protected WordPress option. Care should be taken to not name
344 * options the same as the ones which are protected.
348 * @param string $option Name of option to add. Expected to not be SQL-escaped.
349 * @param mixed $value Optional. Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
350 * @param mixed $deprecated Optional. Description. Not used anymore.
351 * @param bool $autoload Optional. Default is enabled. Whether to load the option when WordPress starts up.
352 * @return bool False if option was not added and true if option was added.
354 function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) {
357 if ( !empty( $deprecated ) )
358 _deprecated_argument( __FUNCTION__, '2.3' );
360 $option = trim($option);
361 if ( empty($option) )
364 wp_protect_special_option( $option );
366 if ( is_object($value) )
367 $value = clone $value;
369 $value = sanitize_option( $option, $value );
371 // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
372 $notoptions = wp_cache_get( 'notoptions', 'options' );
373 if ( !is_array( $notoptions ) || !isset( $notoptions[$option] ) )
374 if ( false !== get_option( $option ) )
377 $serialized_value = maybe_serialize( $value );
378 $autoload = ( 'no' === $autoload ) ? 'no' : 'yes';
381 * Fires before an option is added.
385 * @param string $option Name of the option to add.
386 * @param mixed $value Value of the option.
388 do_action( 'add_option', $option, $value );
390 $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 ) );
394 if ( ! defined( 'WP_INSTALLING' ) ) {
395 if ( 'yes' == $autoload ) {
396 $alloptions = wp_load_alloptions();
397 $alloptions[ $option ] = $serialized_value;
398 wp_cache_set( 'alloptions', $alloptions, 'options' );
400 wp_cache_set( $option, $serialized_value, 'options' );
404 // This option exists now
405 $notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh
406 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
407 unset( $notoptions[$option] );
408 wp_cache_set( 'notoptions', $notoptions, 'options' );
412 * Fires after a specific option has been added.
414 * The dynamic portion of the hook name, $option, refers to the option name.
416 * @since 2.5.0 As "add_option_{$name}"
419 * @param string $option Name of the option to add.
420 * @param mixed $value Value of the option.
422 do_action( "add_option_{$option}", $option, $value );
425 * Fires after an option has been added.
429 * @param string $option Name of the added option.
430 * @param mixed $value Value of the option.
432 do_action( 'added_option', $option, $value );
437 * Removes option by name. Prevents removal of protected WordPress options.
441 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
442 * @return bool True, if option is successfully deleted. False on failure.
444 function delete_option( $option ) {
447 $option = trim( $option );
448 if ( empty( $option ) )
451 wp_protect_special_option( $option );
453 // Get the ID, if no ID then return
454 $row = $wpdb->get_row( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) );
455 if ( is_null( $row ) )
459 * Fires immediately before an option is deleted.
463 * @param string $option Name of the option to delete.
465 do_action( 'delete_option', $option );
467 $result = $wpdb->delete( $wpdb->options, array( 'option_name' => $option ) );
468 if ( ! defined( 'WP_INSTALLING' ) ) {
469 if ( 'yes' == $row->autoload ) {
470 $alloptions = wp_load_alloptions();
471 if ( is_array( $alloptions ) && isset( $alloptions[$option] ) ) {
472 unset( $alloptions[$option] );
473 wp_cache_set( 'alloptions', $alloptions, 'options' );
476 wp_cache_delete( $option, 'options' );
482 * Fires after a specific option has been deleted.
484 * The dynamic portion of the hook name, $option, refers to the option name.
488 * @param string $option Name of the deleted option.
490 do_action( "delete_option_$option", $option );
493 * Fires after an option has been deleted.
497 * @param string $option Name of the deleted option.
499 do_action( 'deleted_option', $option );
506 * Delete a transient.
510 * @param string $transient Transient name. Expected to not be SQL-escaped.
511 * @return bool true if successful, false otherwise
513 function delete_transient( $transient ) {
516 * Fires immediately before a specific transient is deleted.
518 * The dynamic portion of the hook name, $transient, refers to the transient name.
522 * @param string $transient Transient name.
524 do_action( 'delete_transient_' . $transient, $transient );
526 if ( wp_using_ext_object_cache() ) {
527 $result = wp_cache_delete( $transient, 'transient' );
529 $option_timeout = '_transient_timeout_' . $transient;
530 $option = '_transient_' . $transient;
531 $result = delete_option( $option );
533 delete_option( $option_timeout );
539 * Fires after a transient is deleted.
543 * @param string $transient Deleted transient name.
545 do_action( 'deleted_transient', $transient );
552 * Get the value of a transient.
554 * If the transient does not exist or does not have a value, then the return value
559 * @param string $transient Transient name. Expected to not be SQL-escaped
560 * @return mixed Value of transient
562 function get_transient( $transient ) {
565 * Filter the value of an existing transient.
567 * The dynamic portion of the hook name, $transient, refers to the transient name.
569 * Passing a truthy value to the filter will effectively short-circuit retrieval
570 * of the transient, returning the passed value instead.
574 * @param mixed $pre_transient The default value to return if the transient does not exist.
575 * Any value other than false will short-circuit the retrieval
576 * of the transient, and return the returned value.
578 $pre = apply_filters( 'pre_transient_' . $transient, false );
579 if ( false !== $pre )
582 if ( wp_using_ext_object_cache() ) {
583 $value = wp_cache_get( $transient, 'transient' );
585 $transient_option = '_transient_' . $transient;
586 if ( ! defined( 'WP_INSTALLING' ) ) {
587 // If option is not in alloptions, it is not autoloaded and thus has a timeout
588 $alloptions = wp_load_alloptions();
589 if ( !isset( $alloptions[$transient_option] ) ) {
590 $transient_timeout = '_transient_timeout_' . $transient;
591 if ( get_option( $transient_timeout ) < time() ) {
592 delete_option( $transient_option );
593 delete_option( $transient_timeout );
599 if ( ! isset( $value ) )
600 $value = get_option( $transient_option );
604 * Filter an existing transient's value.
606 * The dynamic portion of the hook name, $transient, refers to the transient name.
610 * @param mixed $value Value of transient.
612 return apply_filters( 'transient_' . $transient, $value );
616 * Set/update the value of a transient.
618 * You do not need to serialize values. If the value needs to be serialized, then
619 * it will be serialized before it is set.
623 * @param string $transient Transient name. Expected to not be SQL-escaped.
624 * @param mixed $value Transient value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
625 * @param int $expiration Time until expiration in seconds, default 0
626 * @return bool False if value was not set and true if value was set.
628 function set_transient( $transient, $value, $expiration = 0 ) {
631 * Filter a specific transient before its value is set.
633 * The dynamic portion of the hook name, $transient, refers to the transient name.
637 * @param mixed $value New value of transient.
639 $value = apply_filters( 'pre_set_transient_' . $transient, $value );
641 $expiration = (int) $expiration;
643 if ( wp_using_ext_object_cache() ) {
644 $result = wp_cache_set( $transient, $value, 'transient', $expiration );
646 $transient_timeout = '_transient_timeout_' . $transient;
647 $transient = '_transient_' . $transient;
648 if ( false === get_option( $transient ) ) {
652 add_option( $transient_timeout, time() + $expiration, '', 'no' );
654 $result = add_option( $transient, $value, '', $autoload );
656 // If expiration is requested, but the transient has no timeout option,
657 // delete, then re-create transient rather than update.
660 if ( false === get_option( $transient_timeout ) ) {
661 delete_option( $transient );
662 add_option( $transient_timeout, time() + $expiration, '', 'no' );
663 $result = add_option( $transient, $value, '', 'no' );
666 update_option( $transient_timeout, time() + $expiration );
670 $result = update_option( $transient, $value );
678 * Fires after the value for a specific transient has been set.
680 * The dynamic portion of the hook name, $transient, refers to the transient name.
684 * @param mixed $value Transient value.
685 * @param int $expiration Time until expiration in seconds. Default 0.
687 do_action( 'set_transient_' . $transient, $value, $expiration );
690 * Fires after the value for a transient has been set.
694 * @param string $transient The name of the transient.
695 * @param mixed $value Transient value.
696 * @param int $expiration Time until expiration in seconds. Default 0.
698 do_action( 'setted_transient', $transient, $value, $expiration );
704 * Saves and restores user interface settings stored in a cookie.
706 * Checks if the current user-settings cookie is updated and stores it. When no
707 * cookie exists (different browser used), adds the last saved cookie restoring
712 function wp_user_settings() {
717 if ( defined('DOING_AJAX') )
720 if ( ! $user_id = get_current_user_id() )
723 if ( is_super_admin() && ! is_user_member_of_blog() )
726 $settings = (string) get_user_option( 'user-settings', $user_id );
728 if ( isset( $_COOKIE['wp-settings-' . $user_id] ) ) {
729 $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user_id] );
731 // No change or both empty
732 if ( $cookie == $settings )
735 $last_saved = (int) get_user_option( 'user-settings-time', $user_id );
736 $current = isset( $_COOKIE['wp-settings-time-' . $user_id]) ? preg_replace( '/[^0-9]/', '', $_COOKIE['wp-settings-time-' . $user_id] ) : 0;
738 // The cookie is newer than the saved value. Update the user_option and leave the cookie as-is
739 if ( $current > $last_saved ) {
740 update_user_option( $user_id, 'user-settings', $cookie, false );
741 update_user_option( $user_id, 'user-settings-time', time() - 5, false );
746 // The cookie is not set in the current browser or the saved value is newer.
747 setcookie( 'wp-settings-' . $user_id, $settings, time() + YEAR_IN_SECONDS, SITECOOKIEPATH );
748 setcookie( 'wp-settings-time-' . $user_id, time(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH );
749 $_COOKIE['wp-settings-' . $user_id] = $settings;
753 * Retrieve user interface setting value based on setting name.
757 * @param string $name The name of the setting.
758 * @param string $default Optional default value to return when $name is not set.
759 * @return mixed the last saved user setting or the default value/false if it doesn't exist.
761 function get_user_setting( $name, $default = false ) {
762 $all_user_settings = get_all_user_settings();
764 return isset( $all_user_settings[$name] ) ? $all_user_settings[$name] : $default;
768 * Add or update user interface setting.
770 * Both $name and $value can contain only ASCII letters, numbers and underscores.
771 * This function has to be used before any output has started as it calls setcookie().
775 * @param string $name The name of the setting.
776 * @param string $value The value for the setting.
777 * @return bool true if set successfully/false if not.
779 function set_user_setting( $name, $value ) {
781 if ( headers_sent() )
784 $all_user_settings = get_all_user_settings();
785 $all_user_settings[$name] = $value;
787 return wp_set_all_user_settings( $all_user_settings );
791 * Delete user interface settings.
793 * Deleting settings would reset them to the defaults.
794 * This function has to be used before any output has started as it calls setcookie().
798 * @param mixed $names The name or array of names of the setting to be deleted.
799 * @return bool true if deleted successfully/false if not.
801 function delete_user_setting( $names ) {
803 if ( headers_sent() )
806 $all_user_settings = get_all_user_settings();
807 $names = (array) $names;
810 foreach ( $names as $name ) {
811 if ( isset( $all_user_settings[$name] ) ) {
812 unset( $all_user_settings[$name] );
818 return wp_set_all_user_settings( $all_user_settings );
824 * Retrieve all user interface settings.
828 * @return array the last saved user settings or empty array.
830 function get_all_user_settings() {
831 global $_updated_user_settings;
833 if ( ! $user_id = get_current_user_id() )
836 if ( isset( $_updated_user_settings ) && is_array( $_updated_user_settings ) )
837 return $_updated_user_settings;
839 $user_settings = array();
840 if ( isset( $_COOKIE['wp-settings-' . $user_id] ) ) {
841 $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user_id] );
843 if ( $cookie && strpos( $cookie, '=' ) ) // '=' cannot be 1st char
844 parse_str( $cookie, $user_settings );
847 $option = get_user_option( 'user-settings', $user_id );
848 if ( $option && is_string($option) )
849 parse_str( $option, $user_settings );
852 $_updated_user_settings = $user_settings;
853 return $user_settings;
857 * Private. Set all user interface settings.
861 * @param array $user_settings
864 function wp_set_all_user_settings( $user_settings ) {
865 global $_updated_user_settings;
867 if ( ! $user_id = get_current_user_id() )
870 if ( is_super_admin() && ! is_user_member_of_blog() )
874 foreach ( $user_settings as $name => $value ) {
875 $_name = preg_replace( '/[^A-Za-z0-9_]+/', '', $name );
876 $_value = preg_replace( '/[^A-Za-z0-9_]+/', '', $value );
878 if ( ! empty( $_name ) )
879 $settings .= $_name . '=' . $_value . '&';
882 $settings = rtrim($settings, '&');
883 parse_str( $settings, $_updated_user_settings );
885 update_user_option( $user_id, 'user-settings', $settings, false );
886 update_user_option( $user_id, 'user-settings-time', time(), false );
892 * Delete the user settings of the current user.
896 function delete_all_user_settings() {
897 if ( ! $user_id = get_current_user_id() )
900 update_user_option( $user_id, 'user-settings', '', false );
901 setcookie('wp-settings-' . $user_id, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH);
905 * Retrieve site option value based on name of option.
911 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
912 * @param mixed $default Optional value to return if option doesn't exist. Default false.
913 * @param bool $use_cache Whether to use cache. Multisite only. Default true.
914 * @return mixed Value set for the option.
916 function get_site_option( $option, $default = false, $use_cache = true ) {
920 * Filter an existing site option before it is retrieved.
922 * The dynamic portion of the hook name, $option, refers to the option name.
924 * Passing a truthy value to the filter will effectively short-circuit retrieval,
925 * returning the passed value instead.
927 * @since 2.9.0 As 'pre_site_option_' . $key
930 * @param mixed $pre_option The default value to return if the option does not exist.
932 $pre = apply_filters( 'pre_site_option_' . $option, false );
934 if ( false !== $pre )
937 // prevent non-existent options from triggering multiple queries
938 $notoptions_key = "{$wpdb->siteid}:notoptions";
939 $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
941 if ( isset( $notoptions[$option] ) ) {
944 * Filter a specific default site option.
946 * The dynamic portion of the hook name, $option, refers to the option name.
950 * @param mixed $default The value to return if the site option does not exist
953 return apply_filters( 'default_site_option_' . $option, $default );
956 if ( ! is_multisite() ) {
958 /** This filter is documented in wp-includes/option.php */
959 $default = apply_filters( 'default_site_option_' . $option, $default );
960 $value = get_option($option, $default);
962 $cache_key = "{$wpdb->siteid}:$option";
964 $value = wp_cache_get($cache_key, 'site-options');
966 if ( !isset($value) || (false === $value) ) {
967 $row = $wpdb->get_row( $wpdb->prepare("SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );
969 // Has to be get_row instead of get_var because of funkiness with 0, false, null values
970 if ( is_object( $row ) ) {
971 $value = $row->meta_value;
972 $value = maybe_unserialize( $value );
973 wp_cache_set( $cache_key, $value, 'site-options' );
975 $notoptions[$option] = true;
976 wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
978 /** This filter is documented in wp-includes/option.php */
979 $value = apply_filters( 'default_site_option_' . $option, $default );
985 * Filter the value of an existing site option.
987 * The dynamic portion of the hook name, $option, refers to the option name.
989 * @since 2.9.0 As 'site_option_' . $key
992 * @param mixed $value Value of site option.
994 return apply_filters( 'site_option_' . $option, $value );
998 * Add a new site option.
1000 * Existing options will not be updated. Note that prior to 3.3 this wasn't the case.
1006 * @param string $option Name of option to add. Expected to not be SQL-escaped.
1007 * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
1008 * @return bool False if option was not added and true if option was added.
1010 function add_site_option( $option, $value ) {
1013 wp_protect_special_option( $option );
1016 * Filter the value of a specific site option before it is added.
1018 * The dynamic portion of the hook name, $option, refers to the option name.
1020 * @since 2.9.0 As 'pre_add_site_option_' . $key
1023 * @param mixed $value Value of site option.
1025 $value = apply_filters( 'pre_add_site_option_' . $option, $value );
1027 $notoptions_key = "{$wpdb->siteid}:notoptions";
1029 if ( !is_multisite() ) {
1030 $result = add_option( $option, $value );
1032 $cache_key = "{$wpdb->siteid}:$option";
1034 // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
1035 $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
1036 if ( ! is_array( $notoptions ) || ! isset( $notoptions[$option] ) )
1037 if ( false !== get_site_option( $option ) )
1040 $value = sanitize_option( $option, $value );
1042 $serialized_value = maybe_serialize( $value );
1043 $result = $wpdb->insert( $wpdb->sitemeta, array('site_id' => $wpdb->siteid, 'meta_key' => $option, 'meta_value' => $serialized_value ) );
1048 wp_cache_set( $cache_key, $value, 'site-options' );
1050 // This option exists now
1051 $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); // yes, again... we need it to be fresh
1052 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
1053 unset( $notoptions[$option] );
1054 wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
1061 * Fires after a specific site option has been successfully added.
1063 * The dynamic portion of the hook name, $option, refers to the option name.
1065 * @since 2.9.0 As "add_site_option_{$key}"
1068 * @param string $option Name of site option.
1069 * @param mixed $value Value of site option.
1071 do_action( "add_site_option_{$option}", $option, $value );
1074 * Fires after a site option has been successfully added.
1078 * @param string $option Name of site option.
1079 * @param mixed $value Value of site option.
1081 do_action( "add_site_option", $option, $value );
1089 * Removes site option by name.
1093 * @see delete_option()
1095 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
1096 * @return bool True, if succeed. False, if failure.
1098 function delete_site_option( $option ) {
1101 // ms_protect_special_option( $option ); @todo
1104 * Fires immediately before a specific site option is deleted.
1106 * The dynamic portion of the hook name, $option, refers to the option name.
1110 do_action( 'pre_delete_site_option_' . $option );
1112 if ( !is_multisite() ) {
1113 $result = delete_option( $option );
1115 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );
1116 if ( is_null( $row ) || !$row->meta_id )
1118 $cache_key = "{$wpdb->siteid}:$option";
1119 wp_cache_delete( $cache_key, 'site-options' );
1121 $result = $wpdb->delete( $wpdb->sitemeta, array( 'meta_key' => $option, 'site_id' => $wpdb->siteid ) );
1127 * Fires after a specific site option has been deleted.
1129 * The dynamic portion of the hook name, $option, refers to the option name.
1131 * @since 2.9.0 As "delete_site_option_{$key}"
1134 * @param string $option Name of the site option.
1136 do_action( "delete_site_option_{$option}", $option );
1139 * Fires after a site option has been deleted.
1143 * @param string $option Name of the site option.
1145 do_action( "delete_site_option", $option );
1153 * Update the value of a site option that was already added.
1157 * @see update_option()
1159 * @param string $option Name of option. Expected to not be SQL-escaped.
1160 * @param mixed $value Option value. Expected to not be SQL-escaped.
1161 * @return bool False if value was not updated and true if value was updated.
1163 function update_site_option( $option, $value ) {
1166 wp_protect_special_option( $option );
1168 $old_value = get_site_option( $option );
1171 * Filter a specific site option before its value is updated.
1173 * The dynamic portion of the hook name, $option, refers to the option name.
1175 * @since 2.9.0 As 'pre_update_site_option_' . $key
1178 * @param mixed $value New value of site option.
1179 * @param mixed $old_value Old value of site option.
1181 $value = apply_filters( 'pre_update_site_option_' . $option, $value, $old_value );
1183 if ( $value === $old_value )
1186 if ( false === $old_value )
1187 return add_site_option( $option, $value );
1189 $notoptions_key = "{$wpdb->siteid}:notoptions";
1190 $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
1191 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
1192 unset( $notoptions[$option] );
1193 wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
1196 if ( !is_multisite() ) {
1197 $result = update_option( $option, $value );
1199 $value = sanitize_option( $option, $value );
1201 $serialized_value = maybe_serialize( $value );
1202 $result = $wpdb->update( $wpdb->sitemeta, array( 'meta_value' => $serialized_value ), array( 'site_id' => $wpdb->siteid, 'meta_key' => $option ) );
1205 $cache_key = "{$wpdb->siteid}:$option";
1206 wp_cache_set( $cache_key, $value, 'site-options' );
1213 * Fires after the value of a specific site option has been successfully updated.
1215 * The dynamic portion of the hook name, $option, refers to the option name.
1217 * @since 2.9.0 As "update_site_option_{$key}"
1220 * @param string $option Name of site option.
1221 * @param mixed $value Current value of site option.
1222 * @param mixed $old_value Old value of site option.
1224 do_action( "update_site_option_{$option}", $option, $value, $old_value );
1227 * Fires after the value of a site option has been successfully updated.
1231 * @param string $option Name of site option.
1232 * @param mixed $value Current value of site option.
1233 * @param mixed $old_value Old value of site option.
1235 do_action( "update_site_option", $option, $value, $old_value );
1243 * Delete a site transient.
1247 * @param string $transient Transient name. Expected to not be SQL-escaped.
1248 * @return bool True if successful, false otherwise
1250 function delete_site_transient( $transient ) {
1253 * Fires immediately before a specific site transient is deleted.
1255 * The dynamic portion of the hook name, $transient, refers to the transient name.
1259 * @param string $transient Transient name.
1261 do_action( 'delete_site_transient_' . $transient, $transient );
1263 if ( wp_using_ext_object_cache() ) {
1264 $result = wp_cache_delete( $transient, 'site-transient' );
1266 $option_timeout = '_site_transient_timeout_' . $transient;
1267 $option = '_site_transient_' . $transient;
1268 $result = delete_site_option( $option );
1270 delete_site_option( $option_timeout );
1275 * Fires after a transient is deleted.
1279 * @param string $transient Deleted transient name.
1281 do_action( 'deleted_site_transient', $transient );
1288 * Get the value of a site transient.
1290 * If the transient does not exist or does not have a value, then the return value
1295 * @see get_transient()
1297 * @param string $transient Transient name. Expected to not be SQL-escaped.
1298 * @return mixed Value of transient
1300 function get_site_transient( $transient ) {
1303 * Filter the value of an existing site transient.
1305 * The dynamic portion of the hook name, $transient, refers to the transient name.
1307 * Passing a truthy value to the filter will effectively short-circuit retrieval,
1308 * returning the passed value instead.
1312 * @param mixed $pre_site_transient The default value to return if the site transient does not exist.
1313 * Any value other than false will short-circuit the retrieval
1314 * of the transient, and return the returned value.
1316 $pre = apply_filters( 'pre_site_transient_' . $transient, false );
1318 if ( false !== $pre )
1321 if ( wp_using_ext_object_cache() ) {
1322 $value = wp_cache_get( $transient, 'site-transient' );
1324 // Core transients that do not have a timeout. Listed here so querying timeouts can be avoided.
1325 $no_timeout = array('update_core', 'update_plugins', 'update_themes');
1326 $transient_option = '_site_transient_' . $transient;
1327 if ( ! in_array( $transient, $no_timeout ) ) {
1328 $transient_timeout = '_site_transient_timeout_' . $transient;
1329 $timeout = get_site_option( $transient_timeout );
1330 if ( false !== $timeout && $timeout < time() ) {
1331 delete_site_option( $transient_option );
1332 delete_site_option( $transient_timeout );
1337 if ( ! isset( $value ) )
1338 $value = get_site_option( $transient_option );
1342 * Filter the value of an existing site transient.
1344 * The dynamic portion of the hook name, $transient, refers to the transient name.
1348 * @param mixed $value Value of site transient.
1350 return apply_filters( 'site_transient_' . $transient, $value );
1354 * Set/update the value of a site transient.
1356 * You do not need to serialize values, if the value needs to be serialize, then
1357 * it will be serialized before it is set.
1361 * @see set_transient()
1363 * @param string $transient Transient name. Expected to not be SQL-escaped.
1364 * @param mixed $value Transient value. Expected to not be SQL-escaped.
1365 * @param int $expiration Time until expiration in seconds, default 0
1366 * @return bool False if value was not set and true if value was set.
1368 function set_site_transient( $transient, $value, $expiration = 0 ) {
1371 * Filter the value of a specific site transient before it is set.
1373 * The dynamic portion of the hook name, $transient, refers to the transient name.
1377 * @param mixed $value Value of site transient.
1379 $value = apply_filters( 'pre_set_site_transient_' . $transient, $value );
1381 $expiration = (int) $expiration;
1383 if ( wp_using_ext_object_cache() ) {
1384 $result = wp_cache_set( $transient, $value, 'site-transient', $expiration );
1386 $transient_timeout = '_site_transient_timeout_' . $transient;
1387 $option = '_site_transient_' . $transient;
1388 if ( false === get_site_option( $option ) ) {
1390 add_site_option( $transient_timeout, time() + $expiration );
1391 $result = add_site_option( $option, $value );
1394 update_site_option( $transient_timeout, time() + $expiration );
1395 $result = update_site_option( $option, $value );
1401 * Fires after the value for a specific site transient has been set.
1403 * The dynamic portion of the hook name, $transient, refers to the transient name.
1407 * @param mixed $value Site transient value.
1408 * @param int $expiration Time until expiration in seconds. Default 0.
1410 do_action( 'set_site_transient_' . $transient, $value, $expiration );
1413 * Fires after the value for a site transient has been set.
1417 * @param string $transient The name of the site transient.
1418 * @param mixed $value Site transient value.
1419 * @param int $expiration Time until expiration in seconds. Default 0.
1421 do_action( 'setted_site_transient', $transient, $value, $expiration );