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, does not have a value, or has expired,
555 * then the return value will be false.
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. Must be
624 * 45 characters or fewer in length.
625 * @param mixed $value Transient value. Must be serializable if non-scalar.
626 * Expected to not be SQL-escaped.
627 * @param int $expiration Optional. Time until expiration in seconds. Default 0.
628 * @return bool False if value was not set and true if value was set.
630 function set_transient( $transient, $value, $expiration = 0 ) {
633 * Filter a specific transient before its value is set.
635 * The dynamic portion of the hook name, $transient, refers to the transient name.
639 * @param mixed $value New value of transient.
641 $value = apply_filters( 'pre_set_transient_' . $transient, $value );
643 $expiration = (int) $expiration;
645 if ( wp_using_ext_object_cache() ) {
646 $result = wp_cache_set( $transient, $value, 'transient', $expiration );
648 $transient_timeout = '_transient_timeout_' . $transient;
649 $transient = '_transient_' . $transient;
650 if ( false === get_option( $transient ) ) {
654 add_option( $transient_timeout, time() + $expiration, '', 'no' );
656 $result = add_option( $transient, $value, '', $autoload );
658 // If expiration is requested, but the transient has no timeout option,
659 // delete, then re-create transient rather than update.
662 if ( false === get_option( $transient_timeout ) ) {
663 delete_option( $transient );
664 add_option( $transient_timeout, time() + $expiration, '', 'no' );
665 $result = add_option( $transient, $value, '', 'no' );
668 update_option( $transient_timeout, time() + $expiration );
672 $result = update_option( $transient, $value );
680 * Fires after the value for a specific transient has been set.
682 * The dynamic portion of the hook name, $transient, refers to the transient name.
686 * @param mixed $value Transient value.
687 * @param int $expiration Time until expiration in seconds. Default 0.
689 do_action( 'set_transient_' . $transient, $value, $expiration );
692 * Fires after the value for a transient has been set.
696 * @param string $transient The name of the transient.
697 * @param mixed $value Transient value.
698 * @param int $expiration Time until expiration in seconds. Default 0.
700 do_action( 'setted_transient', $transient, $value, $expiration );
706 * Saves and restores user interface settings stored in a cookie.
708 * Checks if the current user-settings cookie is updated and stores it. When no
709 * cookie exists (different browser used), adds the last saved cookie restoring
714 function wp_user_settings() {
716 if ( ! is_admin() || defined( 'DOING_AJAX' ) ) {
720 if ( ! $user_id = get_current_user_id() ) {
724 if ( is_super_admin() && ! is_user_member_of_blog() ) {
728 $settings = (string) get_user_option( 'user-settings', $user_id );
730 if ( isset( $_COOKIE['wp-settings-' . $user_id] ) ) {
731 $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user_id] );
733 // No change or both empty
734 if ( $cookie == $settings )
737 $last_saved = (int) get_user_option( 'user-settings-time', $user_id );
738 $current = isset( $_COOKIE['wp-settings-time-' . $user_id]) ? preg_replace( '/[^0-9]/', '', $_COOKIE['wp-settings-time-' . $user_id] ) : 0;
740 // The cookie is newer than the saved value. Update the user_option and leave the cookie as-is
741 if ( $current > $last_saved ) {
742 update_user_option( $user_id, 'user-settings', $cookie, false );
743 update_user_option( $user_id, 'user-settings-time', time() - 5, false );
748 // The cookie is not set in the current browser or the saved value is newer.
749 $secure = ( 'https' === parse_url( site_url(), PHP_URL_SCHEME ) );
750 setcookie( 'wp-settings-' . $user_id, $settings, time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure );
751 setcookie( 'wp-settings-time-' . $user_id, time(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure );
752 $_COOKIE['wp-settings-' . $user_id] = $settings;
756 * Retrieve user interface setting value based on setting name.
760 * @param string $name The name of the setting.
761 * @param string $default Optional default value to return when $name is not set.
762 * @return mixed the last saved user setting or the default value/false if it doesn't exist.
764 function get_user_setting( $name, $default = false ) {
765 $all_user_settings = get_all_user_settings();
767 return isset( $all_user_settings[$name] ) ? $all_user_settings[$name] : $default;
771 * Add or update user interface setting.
773 * Both $name and $value can contain only ASCII letters, numbers and underscores.
774 * This function has to be used before any output has started as it calls setcookie().
778 * @param string $name The name of the setting.
779 * @param string $value The value for the setting.
780 * @return bool true if set successfully/false if not.
782 function set_user_setting( $name, $value ) {
784 if ( headers_sent() ) {
788 $all_user_settings = get_all_user_settings();
789 $all_user_settings[$name] = $value;
791 return wp_set_all_user_settings( $all_user_settings );
795 * Delete user interface settings.
797 * Deleting settings would reset them to the defaults.
798 * This function has to be used before any output has started as it calls setcookie().
802 * @param mixed $names The name or array of names of the setting to be deleted.
803 * @return bool true if deleted successfully/false if not.
805 function delete_user_setting( $names ) {
807 if ( headers_sent() ) {
811 $all_user_settings = get_all_user_settings();
812 $names = (array) $names;
815 foreach ( $names as $name ) {
816 if ( isset( $all_user_settings[$name] ) ) {
817 unset( $all_user_settings[$name] );
823 return wp_set_all_user_settings( $all_user_settings );
830 * Retrieve all user interface settings.
834 * @return array the last saved user settings or empty array.
836 function get_all_user_settings() {
837 global $_updated_user_settings;
839 if ( ! $user_id = get_current_user_id() ) {
843 if ( isset( $_updated_user_settings ) && is_array( $_updated_user_settings ) ) {
844 return $_updated_user_settings;
847 $user_settings = array();
849 if ( isset( $_COOKIE['wp-settings-' . $user_id] ) ) {
850 $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user_id] );
852 if ( strpos( $cookie, '=' ) ) { // '=' cannot be 1st char
853 parse_str( $cookie, $user_settings );
856 $option = get_user_option( 'user-settings', $user_id );
858 if ( $option && is_string( $option ) ) {
859 parse_str( $option, $user_settings );
863 $_updated_user_settings = $user_settings;
864 return $user_settings;
868 * Private. Set all user interface settings.
872 * @param array $user_settings
875 function wp_set_all_user_settings( $user_settings ) {
876 global $_updated_user_settings;
878 if ( ! $user_id = get_current_user_id() ) {
882 if ( is_super_admin() && ! is_user_member_of_blog() ) {
887 foreach ( $user_settings as $name => $value ) {
888 $_name = preg_replace( '/[^A-Za-z0-9_]+/', '', $name );
889 $_value = preg_replace( '/[^A-Za-z0-9_]+/', '', $value );
891 if ( ! empty( $_name ) ) {
892 $settings .= $_name . '=' . $_value . '&';
896 $settings = rtrim( $settings, '&' );
897 parse_str( $settings, $_updated_user_settings );
899 update_user_option( $user_id, 'user-settings', $settings, false );
900 update_user_option( $user_id, 'user-settings-time', time(), false );
906 * Delete the user settings of the current user.
910 function delete_all_user_settings() {
911 if ( ! $user_id = get_current_user_id() ) {
915 update_user_option( $user_id, 'user-settings', '', false );
916 setcookie( 'wp-settings-' . $user_id, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH );
920 * Retrieve site option value based on name of option.
926 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
927 * @param mixed $default Optional value to return if option doesn't exist. Default false.
928 * @param bool $use_cache Whether to use cache. Multisite only. Default true.
929 * @return mixed Value set for the option.
931 function get_site_option( $option, $default = false, $use_cache = true ) {
935 * Filter an existing site option before it is retrieved.
937 * The dynamic portion of the hook name, $option, refers to the option name.
939 * Passing a truthy value to the filter will effectively short-circuit retrieval,
940 * returning the passed value instead.
942 * @since 2.9.0 As 'pre_site_option_' . $key
945 * @param mixed $pre_option The default value to return if the option does not exist.
947 $pre = apply_filters( 'pre_site_option_' . $option, false );
949 if ( false !== $pre )
952 // prevent non-existent options from triggering multiple queries
953 $notoptions_key = "{$wpdb->siteid}:notoptions";
954 $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
956 if ( isset( $notoptions[$option] ) ) {
959 * Filter a specific default site option.
961 * The dynamic portion of the hook name, $option, refers to the option name.
965 * @param mixed $default The value to return if the site option does not exist
968 return apply_filters( 'default_site_option_' . $option, $default );
971 if ( ! is_multisite() ) {
973 /** This filter is documented in wp-includes/option.php */
974 $default = apply_filters( 'default_site_option_' . $option, $default );
975 $value = get_option($option, $default);
977 $cache_key = "{$wpdb->siteid}:$option";
979 $value = wp_cache_get($cache_key, 'site-options');
981 if ( !isset($value) || (false === $value) ) {
982 $row = $wpdb->get_row( $wpdb->prepare("SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );
984 // Has to be get_row instead of get_var because of funkiness with 0, false, null values
985 if ( is_object( $row ) ) {
986 $value = $row->meta_value;
987 $value = maybe_unserialize( $value );
988 wp_cache_set( $cache_key, $value, 'site-options' );
990 $notoptions[$option] = true;
991 wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
993 /** This filter is documented in wp-includes/option.php */
994 $value = apply_filters( 'default_site_option_' . $option, $default );
1000 * Filter the value of an existing site option.
1002 * The dynamic portion of the hook name, $option, refers to the option name.
1004 * @since 2.9.0 As 'site_option_' . $key
1007 * @param mixed $value Value of site option.
1009 return apply_filters( 'site_option_' . $option, $value );
1013 * Add a new site option.
1015 * Existing options will not be updated. Note that prior to 3.3 this wasn't the case.
1021 * @param string $option Name of option to add. Expected to not be SQL-escaped.
1022 * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
1023 * @return bool False if option was not added and true if option was added.
1025 function add_site_option( $option, $value ) {
1028 wp_protect_special_option( $option );
1031 * Filter the value of a specific site option before it is added.
1033 * The dynamic portion of the hook name, $option, refers to the option name.
1035 * @since 2.9.0 As 'pre_add_site_option_' . $key
1038 * @param mixed $value Value of site option.
1040 $value = apply_filters( 'pre_add_site_option_' . $option, $value );
1042 $notoptions_key = "{$wpdb->siteid}:notoptions";
1044 if ( !is_multisite() ) {
1045 $result = add_option( $option, $value );
1047 $cache_key = "{$wpdb->siteid}:$option";
1049 // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
1050 $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
1051 if ( ! is_array( $notoptions ) || ! isset( $notoptions[$option] ) )
1052 if ( false !== get_site_option( $option ) )
1055 $value = sanitize_option( $option, $value );
1057 $serialized_value = maybe_serialize( $value );
1058 $result = $wpdb->insert( $wpdb->sitemeta, array('site_id' => $wpdb->siteid, 'meta_key' => $option, 'meta_value' => $serialized_value ) );
1063 wp_cache_set( $cache_key, $value, 'site-options' );
1065 // This option exists now
1066 $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); // yes, again... we need it to be fresh
1067 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
1068 unset( $notoptions[$option] );
1069 wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
1076 * Fires after a specific site option has been successfully added.
1078 * The dynamic portion of the hook name, $option, refers to the option name.
1080 * @since 2.9.0 As "add_site_option_{$key}"
1083 * @param string $option Name of site option.
1084 * @param mixed $value Value of site option.
1086 do_action( "add_site_option_{$option}", $option, $value );
1089 * Fires after a site option has been successfully added.
1093 * @param string $option Name of site option.
1094 * @param mixed $value Value of site option.
1096 do_action( "add_site_option", $option, $value );
1104 * Removes site option by name.
1108 * @see delete_option()
1110 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
1111 * @return bool True, if succeed. False, if failure.
1113 function delete_site_option( $option ) {
1116 // ms_protect_special_option( $option ); @todo
1119 * Fires immediately before a specific site option is deleted.
1121 * The dynamic portion of the hook name, $option, refers to the option name.
1125 do_action( 'pre_delete_site_option_' . $option );
1127 if ( !is_multisite() ) {
1128 $result = delete_option( $option );
1130 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );
1131 if ( is_null( $row ) || !$row->meta_id )
1133 $cache_key = "{$wpdb->siteid}:$option";
1134 wp_cache_delete( $cache_key, 'site-options' );
1136 $result = $wpdb->delete( $wpdb->sitemeta, array( 'meta_key' => $option, 'site_id' => $wpdb->siteid ) );
1142 * Fires after a specific site option has been deleted.
1144 * The dynamic portion of the hook name, $option, refers to the option name.
1146 * @since 2.9.0 As "delete_site_option_{$key}"
1149 * @param string $option Name of the site option.
1151 do_action( "delete_site_option_{$option}", $option );
1154 * Fires after a site option has been deleted.
1158 * @param string $option Name of the site option.
1160 do_action( "delete_site_option", $option );
1168 * Update the value of a site option that was already added.
1172 * @see update_option()
1174 * @param string $option Name of option. Expected to not be SQL-escaped.
1175 * @param mixed $value Option value. Expected to not be SQL-escaped.
1176 * @return bool False if value was not updated and true if value was updated.
1178 function update_site_option( $option, $value ) {
1181 wp_protect_special_option( $option );
1183 $old_value = get_site_option( $option );
1186 * Filter a specific site option before its value is updated.
1188 * The dynamic portion of the hook name, $option, refers to the option name.
1190 * @since 2.9.0 As 'pre_update_site_option_' . $key
1193 * @param mixed $value New value of site option.
1194 * @param mixed $old_value Old value of site option.
1196 $value = apply_filters( 'pre_update_site_option_' . $option, $value, $old_value );
1198 if ( $value === $old_value )
1201 if ( false === $old_value )
1202 return add_site_option( $option, $value );
1204 $notoptions_key = "{$wpdb->siteid}:notoptions";
1205 $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
1206 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
1207 unset( $notoptions[$option] );
1208 wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
1211 if ( !is_multisite() ) {
1212 $result = update_option( $option, $value );
1214 $value = sanitize_option( $option, $value );
1216 $serialized_value = maybe_serialize( $value );
1217 $result = $wpdb->update( $wpdb->sitemeta, array( 'meta_value' => $serialized_value ), array( 'site_id' => $wpdb->siteid, 'meta_key' => $option ) );
1220 $cache_key = "{$wpdb->siteid}:$option";
1221 wp_cache_set( $cache_key, $value, 'site-options' );
1228 * Fires after the value of a specific site option has been successfully updated.
1230 * The dynamic portion of the hook name, $option, refers to the option name.
1232 * @since 2.9.0 As "update_site_option_{$key}"
1235 * @param string $option Name of site option.
1236 * @param mixed $value Current value of site option.
1237 * @param mixed $old_value Old value of site option.
1239 do_action( "update_site_option_{$option}", $option, $value, $old_value );
1242 * Fires after the value of a site option has been successfully updated.
1246 * @param string $option Name of site option.
1247 * @param mixed $value Current value of site option.
1248 * @param mixed $old_value Old value of site option.
1250 do_action( "update_site_option", $option, $value, $old_value );
1258 * Delete a site transient.
1262 * @param string $transient Transient name. Expected to not be SQL-escaped.
1263 * @return bool True if successful, false otherwise
1265 function delete_site_transient( $transient ) {
1268 * Fires immediately before a specific site transient is deleted.
1270 * The dynamic portion of the hook name, $transient, refers to the transient name.
1274 * @param string $transient Transient name.
1276 do_action( 'delete_site_transient_' . $transient, $transient );
1278 if ( wp_using_ext_object_cache() ) {
1279 $result = wp_cache_delete( $transient, 'site-transient' );
1281 $option_timeout = '_site_transient_timeout_' . $transient;
1282 $option = '_site_transient_' . $transient;
1283 $result = delete_site_option( $option );
1285 delete_site_option( $option_timeout );
1290 * Fires after a transient is deleted.
1294 * @param string $transient Deleted transient name.
1296 do_action( 'deleted_site_transient', $transient );
1303 * Get the value of a site transient.
1305 * If the transient does not exist, does not have a value, or has expired,
1306 * then the return value will be false.
1310 * @see get_transient()
1312 * @param string $transient Transient name. Expected to not be SQL-escaped.
1313 * @return mixed Value of transient.
1315 function get_site_transient( $transient ) {
1318 * Filter the value of an existing site transient.
1320 * The dynamic portion of the hook name, $transient, refers to the transient name.
1322 * Passing a truthy value to the filter will effectively short-circuit retrieval,
1323 * returning the passed value instead.
1327 * @param mixed $pre_site_transient The default value to return if the site transient does not exist.
1328 * Any value other than false will short-circuit the retrieval
1329 * of the transient, and return the returned value.
1331 $pre = apply_filters( 'pre_site_transient_' . $transient, false );
1333 if ( false !== $pre )
1336 if ( wp_using_ext_object_cache() ) {
1337 $value = wp_cache_get( $transient, 'site-transient' );
1339 // Core transients that do not have a timeout. Listed here so querying timeouts can be avoided.
1340 $no_timeout = array('update_core', 'update_plugins', 'update_themes');
1341 $transient_option = '_site_transient_' . $transient;
1342 if ( ! in_array( $transient, $no_timeout ) ) {
1343 $transient_timeout = '_site_transient_timeout_' . $transient;
1344 $timeout = get_site_option( $transient_timeout );
1345 if ( false !== $timeout && $timeout < time() ) {
1346 delete_site_option( $transient_option );
1347 delete_site_option( $transient_timeout );
1352 if ( ! isset( $value ) )
1353 $value = get_site_option( $transient_option );
1357 * Filter the value of an existing site transient.
1359 * The dynamic portion of the hook name, $transient, refers to the transient name.
1363 * @param mixed $value Value of site transient.
1365 return apply_filters( 'site_transient_' . $transient, $value );
1369 * Set/update the value of a site transient.
1371 * You do not need to serialize values, if the value needs to be serialize, then
1372 * it will be serialized before it is set.
1376 * @see set_transient()
1378 * @param string $transient Transient name. Expected to not be SQL-escaped. Must be
1379 * 40 characters or fewer in length.
1380 * @param mixed $value Transient value. Expected to not be SQL-escaped.
1381 * @param int $expiration Optional. Time until expiration in seconds. Default 0.
1382 * @return bool False if value was not set and true if value was set.
1384 function set_site_transient( $transient, $value, $expiration = 0 ) {
1387 * Filter the value of a specific site transient before it is set.
1389 * The dynamic portion of the hook name, $transient, refers to the transient name.
1393 * @param mixed $value Value of site transient.
1395 $value = apply_filters( 'pre_set_site_transient_' . $transient, $value );
1397 $expiration = (int) $expiration;
1399 if ( wp_using_ext_object_cache() ) {
1400 $result = wp_cache_set( $transient, $value, 'site-transient', $expiration );
1402 $transient_timeout = '_site_transient_timeout_' . $transient;
1403 $option = '_site_transient_' . $transient;
1404 if ( false === get_site_option( $option ) ) {
1406 add_site_option( $transient_timeout, time() + $expiration );
1407 $result = add_site_option( $option, $value );
1410 update_site_option( $transient_timeout, time() + $expiration );
1411 $result = update_site_option( $option, $value );
1417 * Fires after the value for a specific site transient has been set.
1419 * The dynamic portion of the hook name, $transient, refers to the transient name.
1423 * @param mixed $value Site transient value.
1424 * @param int $expiration Time until expiration in seconds. Default 0.
1426 do_action( 'set_site_transient_' . $transient, $value, $expiration );
1429 * Fires after the value for a site transient has been set.
1433 * @param string $transient The name of the site transient.
1434 * @param mixed $value Site transient value.
1435 * @param int $expiration Time until expiration in seconds. Default 0.
1437 do_action( 'setted_site_transient', $transient, $value, $expiration );