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 ] ) ) {
57 * Filter the default value for an option.
59 * The dynamic portion of the hook name, `$option`, refers to the option name.
63 * @param mixed $default The default value to return if the option does not exist
66 return apply_filters( 'default_option_' . $option, $default );
69 $alloptions = wp_load_alloptions();
71 if ( isset( $alloptions[$option] ) ) {
72 $value = $alloptions[$option];
74 $value = wp_cache_get( $option, 'options' );
76 if ( false === $value ) {
77 $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
79 // Has to be get_row instead of get_var because of funkiness with 0, false, null values
80 if ( is_object( $row ) ) {
81 $value = $row->option_value;
82 wp_cache_add( $option, $value, 'options' );
83 } else { // option does not exist, so we must cache its non-existence
84 $notoptions[$option] = true;
85 wp_cache_set( 'notoptions', $notoptions, 'options' );
87 /** This filter is documented in wp-includes/option.php */
88 return apply_filters( 'default_option_' . $option, $default );
93 $suppress = $wpdb->suppress_errors();
94 $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
95 $wpdb->suppress_errors( $suppress );
96 if ( is_object( $row ) ) {
97 $value = $row->option_value;
99 /** This filter is documented in wp-includes/option.php */
100 return apply_filters( 'default_option_' . $option, $default );
104 // If home is not set use siteurl.
105 if ( 'home' == $option && '' == $value )
106 return get_option( 'siteurl' );
108 if ( in_array( $option, array('siteurl', 'home', 'category_base', 'tag_base') ) )
109 $value = untrailingslashit( $value );
112 * Filter the value of an existing option.
114 * The dynamic portion of the hook name, `$option`, refers to the option name.
116 * @since 1.5.0 As 'option_' . $setting
119 * @param mixed $value Value of the option. If stored serialized, it will be
120 * unserialized prior to being returned.
122 return apply_filters( 'option_' . $option, maybe_unserialize( $value ) );
126 * Protect WordPress special option from being modified.
128 * Will die if $option is in protected list. Protected options are 'alloptions'
129 * and 'notoptions' options.
133 * @param string $option Option name.
135 function wp_protect_special_option( $option ) {
136 if ( 'alloptions' === $option || 'notoptions' === $option )
137 wp_die( sprintf( __( '%s is a protected WP option and may not be modified' ), esc_html( $option ) ) );
141 * Print option value after sanitizing for forms.
145 * @param string $option Option name.
147 function form_option( $option ) {
148 echo esc_attr( get_option( $option ) );
152 * Loads and caches all autoloaded options, if available or all options.
156 * @return array List of all options.
158 function wp_load_alloptions() {
161 if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
162 $alloptions = wp_cache_get( 'alloptions', 'options' );
166 if ( !$alloptions ) {
167 $suppress = $wpdb->suppress_errors();
168 if ( !$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) )
169 $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
170 $wpdb->suppress_errors($suppress);
171 $alloptions = array();
172 foreach ( (array) $alloptions_db as $o ) {
173 $alloptions[$o->option_name] = $o->option_value;
175 if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
176 wp_cache_add( 'alloptions', $alloptions, 'options' );
183 * Loads and caches certain often requested site options if is_multisite() and a persistent cache is not being used.
187 * @param int $site_id Optional site ID for which to query the options. Defaults to the current site.
189 function wp_load_core_site_options( $site_id = null ) {
192 if ( !is_multisite() || wp_using_ext_object_cache() || defined( 'WP_INSTALLING' ) )
195 if ( empty($site_id) )
196 $site_id = $wpdb->siteid;
198 $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' );
200 $core_options_in = "'" . implode("', '", $core_options) . "'";
201 $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) );
203 foreach ( $options as $option ) {
204 $key = $option->meta_key;
205 $cache_key = "{$site_id}:$key";
206 $option->meta_value = maybe_unserialize( $option->meta_value );
208 wp_cache_set( $cache_key, $option->meta_value, 'site-options' );
213 * Update the value of an option that was already added.
215 * You do not need to serialize values. If the value needs to be serialized, then
216 * it will be serialized before it is inserted into the database. Remember,
217 * resources can not be serialized or added as an option.
219 * If the option does not exist, then the option will be added with the option
220 * value, but you will not be able to set whether it is autoloaded. If you want
221 * to set whether an option is autoloaded, then you need to use the add_option().
225 * @param string $option Option name. Expected to not be SQL-escaped.
226 * @param mixed $value Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
227 * @return bool False if value was not updated and true if value was updated.
229 function update_option( $option, $value ) {
232 $option = trim($option);
233 if ( empty($option) )
236 wp_protect_special_option( $option );
238 if ( is_object( $value ) )
239 $value = clone $value;
241 $value = sanitize_option( $option, $value );
242 $old_value = get_option( $option );
245 * Filter a specific option before its value is (maybe) serialized and updated.
247 * The dynamic portion of the hook name, `$option`, refers to the option name.
251 * @param mixed $value The new, unserialized option value.
252 * @param mixed $old_value The old option value.
254 $value = apply_filters( 'pre_update_option_' . $option, $value, $old_value );
257 * Filter an option before its value is (maybe) serialized and updated.
261 * @param mixed $value The new, unserialized option value.
262 * @param string $option Name of the option.
263 * @param mixed $old_value The old option value.
265 $value = apply_filters( 'pre_update_option', $value, $option, $old_value );
267 // If the new and old values are the same, no need to update.
268 if ( $value === $old_value )
271 if ( false === $old_value )
272 return add_option( $option, $value );
274 $serialized_value = maybe_serialize( $value );
277 * Fires immediately before an option value is updated.
281 * @param string $option Name of the option to update.
282 * @param mixed $old_value The old option value.
283 * @param mixed $value The new option value.
285 do_action( 'update_option', $option, $old_value, $value );
287 $result = $wpdb->update( $wpdb->options, array( 'option_value' => $serialized_value ), array( 'option_name' => $option ) );
291 $notoptions = wp_cache_get( 'notoptions', 'options' );
292 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
293 unset( $notoptions[$option] );
294 wp_cache_set( 'notoptions', $notoptions, 'options' );
297 if ( ! defined( 'WP_INSTALLING' ) ) {
298 $alloptions = wp_load_alloptions();
299 if ( isset( $alloptions[$option] ) ) {
300 $alloptions[ $option ] = $serialized_value;
301 wp_cache_set( 'alloptions', $alloptions, 'options' );
303 wp_cache_set( $option, $serialized_value, 'options' );
308 * Fires after the value of a specific option has been successfully updated.
310 * The dynamic portion of the hook name, `$option`, refers to the option name.
314 * @param mixed $old_value The old option value.
315 * @param mixed $value The new option value.
317 do_action( "update_option_{$option}", $old_value, $value );
320 * Fires after the value of an option has been successfully updated.
324 * @param string $option Name of the updated option.
325 * @param mixed $old_value The old option value.
326 * @param mixed $value The new option value.
328 do_action( 'updated_option', $option, $old_value, $value );
335 * You do not need to serialize values. If the value needs to be serialized, then
336 * it will be serialized before it is inserted into the database. Remember,
337 * resources can not be serialized or added as an option.
339 * You can create options without values and then update the values later.
340 * Existing options will not be updated and checks are performed to ensure that you
341 * aren't adding a protected WordPress option. Care should be taken to not name
342 * options the same as the ones which are protected.
346 * @param string $option Name of option to add. Expected to not be SQL-escaped.
347 * @param mixed $value Optional. Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
348 * @param string $deprecated Optional. Description. Not used anymore.
349 * @param string|bool $autoload Optional. Default is enabled. Whether to load the option when WordPress starts up.
350 * @return bool False if option was not added and true if option was added.
352 function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) {
355 if ( !empty( $deprecated ) )
356 _deprecated_argument( __FUNCTION__, '2.3' );
358 $option = trim($option);
359 if ( empty($option) )
362 wp_protect_special_option( $option );
364 if ( is_object($value) )
365 $value = clone $value;
367 $value = sanitize_option( $option, $value );
369 // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
370 $notoptions = wp_cache_get( 'notoptions', 'options' );
371 if ( !is_array( $notoptions ) || !isset( $notoptions[$option] ) )
372 if ( false !== get_option( $option ) )
375 $serialized_value = maybe_serialize( $value );
376 $autoload = ( 'no' === $autoload ) ? 'no' : 'yes';
379 * Fires before an option is added.
383 * @param string $option Name of the option to add.
384 * @param mixed $value Value of the option.
386 do_action( 'add_option', $option, $value );
388 $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 ) );
392 if ( ! defined( 'WP_INSTALLING' ) ) {
393 if ( 'yes' == $autoload ) {
394 $alloptions = wp_load_alloptions();
395 $alloptions[ $option ] = $serialized_value;
396 wp_cache_set( 'alloptions', $alloptions, 'options' );
398 wp_cache_set( $option, $serialized_value, 'options' );
402 // This option exists now
403 $notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh
404 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
405 unset( $notoptions[$option] );
406 wp_cache_set( 'notoptions', $notoptions, 'options' );
410 * Fires after a specific option has been added.
412 * The dynamic portion of the hook name, `$option`, refers to the option name.
414 * @since 2.5.0 As "add_option_{$name}"
417 * @param string $option Name of the option to add.
418 * @param mixed $value Value of the option.
420 do_action( "add_option_{$option}", $option, $value );
423 * Fires after an option has been added.
427 * @param string $option Name of the added option.
428 * @param mixed $value Value of the option.
430 do_action( 'added_option', $option, $value );
435 * Removes option by name. Prevents removal of protected WordPress options.
439 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
440 * @return bool True, if option is successfully deleted. False on failure.
442 function delete_option( $option ) {
445 $option = trim( $option );
446 if ( empty( $option ) )
449 wp_protect_special_option( $option );
451 // Get the ID, if no ID then return
452 $row = $wpdb->get_row( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) );
453 if ( is_null( $row ) )
457 * Fires immediately before an option is deleted.
461 * @param string $option Name of the option to delete.
463 do_action( 'delete_option', $option );
465 $result = $wpdb->delete( $wpdb->options, array( 'option_name' => $option ) );
466 if ( ! defined( 'WP_INSTALLING' ) ) {
467 if ( 'yes' == $row->autoload ) {
468 $alloptions = wp_load_alloptions();
469 if ( is_array( $alloptions ) && isset( $alloptions[$option] ) ) {
470 unset( $alloptions[$option] );
471 wp_cache_set( 'alloptions', $alloptions, 'options' );
474 wp_cache_delete( $option, 'options' );
480 * Fires after a specific option has been deleted.
482 * The dynamic portion of the hook name, `$option`, refers to the option name.
486 * @param string $option Name of the deleted option.
488 do_action( "delete_option_$option", $option );
491 * Fires after an option has been deleted.
495 * @param string $option Name of the deleted option.
497 do_action( 'deleted_option', $option );
504 * Delete a transient.
508 * @param string $transient Transient name. Expected to not be SQL-escaped.
509 * @return bool true if successful, false otherwise
511 function delete_transient( $transient ) {
514 * Fires immediately before a specific transient is deleted.
516 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
520 * @param string $transient Transient name.
522 do_action( 'delete_transient_' . $transient, $transient );
524 if ( wp_using_ext_object_cache() ) {
525 $result = wp_cache_delete( $transient, 'transient' );
527 $option_timeout = '_transient_timeout_' . $transient;
528 $option = '_transient_' . $transient;
529 $result = delete_option( $option );
531 delete_option( $option_timeout );
537 * Fires after a transient is deleted.
541 * @param string $transient Deleted transient name.
543 do_action( 'deleted_transient', $transient );
550 * Get the value of a transient.
552 * If the transient does not exist, does not have a value, or has expired,
553 * then the return value will be false.
557 * @param string $transient Transient name. Expected to not be SQL-escaped.
558 * @return mixed Value of transient.
560 function get_transient( $transient ) {
563 * Filter the value of an existing transient.
565 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
567 * Passing a truthy value to the filter will effectively short-circuit retrieval
568 * of the transient, returning the passed value instead.
572 * @param mixed $pre_transient The default value to return if the transient does not exist.
573 * Any value other than false will short-circuit the retrieval
574 * of the transient, and return the returned value.
576 $pre = apply_filters( 'pre_transient_' . $transient, false );
577 if ( false !== $pre )
580 if ( wp_using_ext_object_cache() ) {
581 $value = wp_cache_get( $transient, 'transient' );
583 $transient_option = '_transient_' . $transient;
584 if ( ! defined( 'WP_INSTALLING' ) ) {
585 // If option is not in alloptions, it is not autoloaded and thus has a timeout
586 $alloptions = wp_load_alloptions();
587 if ( !isset( $alloptions[$transient_option] ) ) {
588 $transient_timeout = '_transient_timeout_' . $transient;
589 if ( get_option( $transient_timeout ) < time() ) {
590 delete_option( $transient_option );
591 delete_option( $transient_timeout );
597 if ( ! isset( $value ) )
598 $value = get_option( $transient_option );
602 * Filter an existing transient's value.
604 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
608 * @param mixed $value Value of transient.
610 return apply_filters( 'transient_' . $transient, $value );
614 * Set/update the value of a transient.
616 * You do not need to serialize values. If the value needs to be serialized, then
617 * it will be serialized before it is set.
621 * @param string $transient Transient name. Expected to not be SQL-escaped. Must be
622 * 45 characters or fewer in length.
623 * @param mixed $value Transient value. Must be serializable if non-scalar.
624 * Expected to not be SQL-escaped.
625 * @param int $expiration Optional. 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() {
714 if ( ! is_admin() || defined( 'DOING_AJAX' ) ) {
718 if ( ! $user_id = get_current_user_id() ) {
722 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 $secure = ( 'https' === parse_url( site_url(), PHP_URL_SCHEME ) );
748 setcookie( 'wp-settings-' . $user_id, $settings, time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure );
749 setcookie( 'wp-settings-time-' . $user_id, time(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure );
750 $_COOKIE['wp-settings-' . $user_id] = $settings;
754 * Retrieve user interface setting value based on setting name.
758 * @param string $name The name of the setting.
759 * @param string $default Optional default value to return when $name is not set.
760 * @return mixed the last saved user setting or the default value/false if it doesn't exist.
762 function get_user_setting( $name, $default = false ) {
763 $all_user_settings = get_all_user_settings();
765 return isset( $all_user_settings[$name] ) ? $all_user_settings[$name] : $default;
769 * Add or update user interface setting.
771 * Both $name and $value can contain only ASCII letters, numbers and underscores.
772 * This function has to be used before any output has started as it calls setcookie().
776 * @param string $name The name of the setting.
777 * @param string $value The value for the setting.
778 * @return null|bool true if set successfully/false if not.
780 function set_user_setting( $name, $value ) {
782 if ( headers_sent() ) {
786 $all_user_settings = get_all_user_settings();
787 $all_user_settings[$name] = $value;
789 return wp_set_all_user_settings( $all_user_settings );
793 * Delete user interface settings.
795 * Deleting settings would reset them to the defaults.
796 * This function has to be used before any output has started as it calls setcookie().
800 * @param string $names The name or array of names of the setting to be deleted.
801 * @return null|bool true if deleted successfully/false if not.
803 function delete_user_setting( $names ) {
805 if ( headers_sent() ) {
809 $all_user_settings = get_all_user_settings();
810 $names = (array) $names;
813 foreach ( $names as $name ) {
814 if ( isset( $all_user_settings[$name] ) ) {
815 unset( $all_user_settings[$name] );
821 return wp_set_all_user_settings( $all_user_settings );
828 * Retrieve all user interface settings.
832 * @return array the last saved user settings or empty array.
834 function get_all_user_settings() {
835 global $_updated_user_settings;
837 if ( ! $user_id = get_current_user_id() ) {
841 if ( isset( $_updated_user_settings ) && is_array( $_updated_user_settings ) ) {
842 return $_updated_user_settings;
845 $user_settings = array();
847 if ( isset( $_COOKIE['wp-settings-' . $user_id] ) ) {
848 $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user_id] );
850 if ( strpos( $cookie, '=' ) ) { // '=' cannot be 1st char
851 parse_str( $cookie, $user_settings );
854 $option = get_user_option( 'user-settings', $user_id );
856 if ( $option && is_string( $option ) ) {
857 parse_str( $option, $user_settings );
861 $_updated_user_settings = $user_settings;
862 return $user_settings;
866 * Private. Set all user interface settings.
870 * @param array $user_settings
873 function wp_set_all_user_settings( $user_settings ) {
874 global $_updated_user_settings;
876 if ( ! $user_id = get_current_user_id() ) {
880 if ( is_super_admin() && ! is_user_member_of_blog() ) {
885 foreach ( $user_settings as $name => $value ) {
886 $_name = preg_replace( '/[^A-Za-z0-9_]+/', '', $name );
887 $_value = preg_replace( '/[^A-Za-z0-9_]+/', '', $value );
889 if ( ! empty( $_name ) ) {
890 $settings .= $_name . '=' . $_value . '&';
894 $settings = rtrim( $settings, '&' );
895 parse_str( $settings, $_updated_user_settings );
897 update_user_option( $user_id, 'user-settings', $settings, false );
898 update_user_option( $user_id, 'user-settings-time', time(), false );
904 * Delete the user settings of the current user.
908 function delete_all_user_settings() {
909 if ( ! $user_id = get_current_user_id() ) {
913 update_user_option( $user_id, 'user-settings', '', false );
914 setcookie( 'wp-settings-' . $user_id, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH );
918 * Retrieve site option value based on name of option.
924 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
925 * @param mixed $default Optional value to return if option doesn't exist. Default false.
926 * @param bool $use_cache Whether to use cache. Multisite only. Default true.
927 * @return mixed Value set for the option.
929 function get_site_option( $option, $default = false, $use_cache = true ) {
933 * Filter an existing site option before it is retrieved.
935 * The dynamic portion of the hook name, `$option`, refers to the option name.
937 * Passing a truthy value to the filter will effectively short-circuit retrieval,
938 * returning the passed value instead.
940 * @since 2.9.0 As 'pre_site_option_' . $key
943 * @param mixed $pre_option The default value to return if the option does not exist.
945 $pre = apply_filters( 'pre_site_option_' . $option, false );
947 if ( false !== $pre )
950 // prevent non-existent options from triggering multiple queries
951 $notoptions_key = "{$wpdb->siteid}:notoptions";
952 $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
954 if ( isset( $notoptions[$option] ) ) {
957 * Filter a specific default site option.
959 * The dynamic portion of the hook name, `$option`, refers to the option name.
963 * @param mixed $default The value to return if the site option does not exist
966 return apply_filters( 'default_site_option_' . $option, $default );
969 if ( ! is_multisite() ) {
971 /** This filter is documented in wp-includes/option.php */
972 $default = apply_filters( 'default_site_option_' . $option, $default );
973 $value = get_option($option, $default);
975 $cache_key = "{$wpdb->siteid}:$option";
977 $value = wp_cache_get($cache_key, 'site-options');
979 if ( !isset($value) || (false === $value) ) {
980 $row = $wpdb->get_row( $wpdb->prepare("SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );
982 // Has to be get_row instead of get_var because of funkiness with 0, false, null values
983 if ( is_object( $row ) ) {
984 $value = $row->meta_value;
985 $value = maybe_unserialize( $value );
986 wp_cache_set( $cache_key, $value, 'site-options' );
988 $notoptions[$option] = true;
989 wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
991 /** This filter is documented in wp-includes/option.php */
992 $value = apply_filters( 'default_site_option_' . $option, $default );
998 * Filter the value of an existing site option.
1000 * The dynamic portion of the hook name, `$option`, refers to the option name.
1002 * @since 2.9.0 As 'site_option_' . $key
1005 * @param mixed $value Value of site option.
1007 return apply_filters( 'site_option_' . $option, $value );
1011 * Add a new site option.
1013 * Existing options will not be updated. Note that prior to 3.3 this wasn't the case.
1019 * @param string $option Name of option to add. Expected to not be SQL-escaped.
1020 * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
1021 * @return bool False if option was not added and true if option was added.
1023 function add_site_option( $option, $value ) {
1026 wp_protect_special_option( $option );
1029 * Filter the value of a specific site option before it is added.
1031 * The dynamic portion of the hook name, `$option`, refers to the option name.
1033 * @since 2.9.0 As 'pre_add_site_option_' . $key
1036 * @param mixed $value Value of site option.
1038 $value = apply_filters( 'pre_add_site_option_' . $option, $value );
1040 $notoptions_key = "{$wpdb->siteid}:notoptions";
1042 if ( !is_multisite() ) {
1043 $result = add_option( $option, $value );
1045 $cache_key = "{$wpdb->siteid}:$option";
1047 // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
1048 $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
1049 if ( ! is_array( $notoptions ) || ! isset( $notoptions[$option] ) )
1050 if ( false !== get_site_option( $option ) )
1053 $value = sanitize_option( $option, $value );
1055 $serialized_value = maybe_serialize( $value );
1056 $result = $wpdb->insert( $wpdb->sitemeta, array('site_id' => $wpdb->siteid, 'meta_key' => $option, 'meta_value' => $serialized_value ) );
1061 wp_cache_set( $cache_key, $value, 'site-options' );
1063 // This option exists now
1064 $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); // yes, again... we need it to be fresh
1065 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
1066 unset( $notoptions[$option] );
1067 wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
1074 * Fires after a specific site option has been successfully added.
1076 * The dynamic portion of the hook name, `$option`, refers to the option name.
1078 * @since 2.9.0 As "add_site_option_{$key}"
1081 * @param string $option Name of site option.
1082 * @param mixed $value Value of site option.
1084 do_action( "add_site_option_{$option}", $option, $value );
1087 * Fires after a site option has been successfully added.
1091 * @param string $option Name of site option.
1092 * @param mixed $value Value of site option.
1094 do_action( "add_site_option", $option, $value );
1102 * Removes site option by name.
1106 * @see delete_option()
1108 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
1109 * @return bool True, if succeed. False, if failure.
1111 function delete_site_option( $option ) {
1114 // ms_protect_special_option( $option ); @todo
1117 * Fires immediately before a specific site option is deleted.
1119 * The dynamic portion of the hook name, `$option`, refers to the option name.
1123 do_action( 'pre_delete_site_option_' . $option );
1125 if ( !is_multisite() ) {
1126 $result = delete_option( $option );
1128 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );
1129 if ( is_null( $row ) || !$row->meta_id )
1131 $cache_key = "{$wpdb->siteid}:$option";
1132 wp_cache_delete( $cache_key, 'site-options' );
1134 $result = $wpdb->delete( $wpdb->sitemeta, array( 'meta_key' => $option, 'site_id' => $wpdb->siteid ) );
1140 * Fires after a specific site option has been deleted.
1142 * The dynamic portion of the hook name, `$option`, refers to the option name.
1144 * @since 2.9.0 As "delete_site_option_{$key}"
1147 * @param string $option Name of the site option.
1149 do_action( "delete_site_option_{$option}", $option );
1152 * Fires after a site option has been deleted.
1156 * @param string $option Name of the site option.
1158 do_action( "delete_site_option", $option );
1166 * Update the value of a site option that was already added.
1170 * @see update_option()
1172 * @param string $option Name of option. Expected to not be SQL-escaped.
1173 * @param mixed $value Option value. Expected to not be SQL-escaped.
1174 * @return bool False if value was not updated and true if value was updated.
1176 function update_site_option( $option, $value ) {
1179 wp_protect_special_option( $option );
1181 $old_value = get_site_option( $option );
1184 * Filter a specific site option before its value is updated.
1186 * The dynamic portion of the hook name, `$option`, refers to the option name.
1188 * @since 2.9.0 As 'pre_update_site_option_' . $key
1191 * @param mixed $value New value of site option.
1192 * @param mixed $old_value Old value of site option.
1194 $value = apply_filters( 'pre_update_site_option_' . $option, $value, $old_value );
1196 if ( $value === $old_value )
1199 if ( false === $old_value )
1200 return add_site_option( $option, $value );
1202 $notoptions_key = "{$wpdb->siteid}:notoptions";
1203 $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
1204 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
1205 unset( $notoptions[$option] );
1206 wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
1209 if ( !is_multisite() ) {
1210 $result = update_option( $option, $value );
1212 $value = sanitize_option( $option, $value );
1214 $serialized_value = maybe_serialize( $value );
1215 $result = $wpdb->update( $wpdb->sitemeta, array( 'meta_value' => $serialized_value ), array( 'site_id' => $wpdb->siteid, 'meta_key' => $option ) );
1218 $cache_key = "{$wpdb->siteid}:$option";
1219 wp_cache_set( $cache_key, $value, 'site-options' );
1226 * Fires after the value of a specific site option has been successfully updated.
1228 * The dynamic portion of the hook name, `$option`, refers to the option name.
1230 * @since 2.9.0 As "update_site_option_{$key}"
1233 * @param string $option Name of site option.
1234 * @param mixed $value Current value of site option.
1235 * @param mixed $old_value Old value of site option.
1237 do_action( "update_site_option_{$option}", $option, $value, $old_value );
1240 * Fires after the value of a site option has been successfully updated.
1244 * @param string $option Name of site option.
1245 * @param mixed $value Current value of site option.
1246 * @param mixed $old_value Old value of site option.
1248 do_action( "update_site_option", $option, $value, $old_value );
1256 * Delete a site transient.
1260 * @param string $transient Transient name. Expected to not be SQL-escaped.
1261 * @return bool True if successful, false otherwise
1263 function delete_site_transient( $transient ) {
1266 * Fires immediately before a specific site transient is deleted.
1268 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1272 * @param string $transient Transient name.
1274 do_action( 'delete_site_transient_' . $transient, $transient );
1276 if ( wp_using_ext_object_cache() ) {
1277 $result = wp_cache_delete( $transient, 'site-transient' );
1279 $option_timeout = '_site_transient_timeout_' . $transient;
1280 $option = '_site_transient_' . $transient;
1281 $result = delete_site_option( $option );
1283 delete_site_option( $option_timeout );
1288 * Fires after a transient is deleted.
1292 * @param string $transient Deleted transient name.
1294 do_action( 'deleted_site_transient', $transient );
1301 * Get the value of a site transient.
1303 * If the transient does not exist, does not have a value, or has expired,
1304 * then the return value will be false.
1308 * @see get_transient()
1310 * @param string $transient Transient name. Expected to not be SQL-escaped.
1311 * @return mixed Value of transient.
1313 function get_site_transient( $transient ) {
1316 * Filter the value of an existing site transient.
1318 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1320 * Passing a truthy value to the filter will effectively short-circuit retrieval,
1321 * returning the passed value instead.
1325 * @param mixed $pre_site_transient The default value to return if the site transient does not exist.
1326 * Any value other than false will short-circuit the retrieval
1327 * of the transient, and return the returned value.
1329 $pre = apply_filters( 'pre_site_transient_' . $transient, false );
1331 if ( false !== $pre )
1334 if ( wp_using_ext_object_cache() ) {
1335 $value = wp_cache_get( $transient, 'site-transient' );
1337 // Core transients that do not have a timeout. Listed here so querying timeouts can be avoided.
1338 $no_timeout = array('update_core', 'update_plugins', 'update_themes');
1339 $transient_option = '_site_transient_' . $transient;
1340 if ( ! in_array( $transient, $no_timeout ) ) {
1341 $transient_timeout = '_site_transient_timeout_' . $transient;
1342 $timeout = get_site_option( $transient_timeout );
1343 if ( false !== $timeout && $timeout < time() ) {
1344 delete_site_option( $transient_option );
1345 delete_site_option( $transient_timeout );
1350 if ( ! isset( $value ) )
1351 $value = get_site_option( $transient_option );
1355 * Filter the value of an existing site transient.
1357 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1361 * @param mixed $value Value of site transient.
1363 return apply_filters( 'site_transient_' . $transient, $value );
1367 * Set/update the value of a site transient.
1369 * You do not need to serialize values, if the value needs to be serialize, then
1370 * it will be serialized before it is set.
1374 * @see set_transient()
1376 * @param string $transient Transient name. Expected to not be SQL-escaped. Must be
1377 * 40 characters or fewer in length.
1378 * @param mixed $value Transient value. Expected to not be SQL-escaped.
1379 * @param int $expiration Optional. Time until expiration in seconds. Default 0.
1380 * @return bool False if value was not set and true if value was set.
1382 function set_site_transient( $transient, $value, $expiration = 0 ) {
1385 * Filter the value of a specific site transient before it is set.
1387 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1391 * @param mixed $value Value of site transient.
1393 $value = apply_filters( 'pre_set_site_transient_' . $transient, $value );
1395 $expiration = (int) $expiration;
1397 if ( wp_using_ext_object_cache() ) {
1398 $result = wp_cache_set( $transient, $value, 'site-transient', $expiration );
1400 $transient_timeout = '_site_transient_timeout_' . $transient;
1401 $option = '_site_transient_' . $transient;
1402 if ( false === get_site_option( $option ) ) {
1404 add_site_option( $transient_timeout, time() + $expiration );
1405 $result = add_site_option( $option, $value );
1408 update_site_option( $transient_timeout, time() + $expiration );
1409 $result = update_site_option( $option, $value );
1415 * Fires after the value for a specific site transient has been set.
1417 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1421 * @param mixed $value Site transient value.
1422 * @param int $expiration Time until expiration in seconds. Default 0.
1424 do_action( 'set_site_transient_' . $transient, $value, $expiration );
1427 * Fires after the value for a site transient has been set.
1431 * @param string $transient The name of the site transient.
1432 * @param mixed $value Site transient value.
1433 * @param int $expiration Time until expiration in seconds. Default 0.
1435 do_action( 'setted_site_transient', $transient, $value, $expiration );