update_option()
update_option( string $option, mixed $value, string|boo…
update_option( string $option, mixed $value, string|bool $autoload = null )
更新已添加的选项的值。
Updates the value of an option that was already added.
目录锚点:#说明#参数#源码#笔记
说明(Description)
不需要序列化值。如果需要序列化该值,则在将其插入数据库之前将其序列化。请记住,资源不能序列化或作为选项添加。如果选项不存在,则将创建该选项。此功能设计为在有或无登录用户的情况下工作。在安全性方面,插件开发人员应该在更新任何选项之前检查当前用户的能力。
参数(Parameters)
参数 | 类型 | 说明 |
---|---|---|
$option | (string) | 选项名称。不应为SQL转义。 |
$value | (mixed) | 选项值。如果不是标量,则必须是可序列化的。不应为SQL转义。 |
$autoload | (string | bool) | WordPress启动时是否加载该选项。对于现有选项,$autoload只能在$value也更改的情况下使用update_option()更新。接受’yes’| true启用或’no’| false禁用。对于不存在的选项,默认值为“是”。 |
源码(Source)
/** * Update the value of an option that was already added. * * You do not need to serialize values. If the value needs to be serialized, then * it will be serialized before it is inserted into the database. Remember, * resources can not be serialized or added as an option. * * If the option does not exist, then the option will be added with the option value, * with an `$autoload` value of 'yes'. * * @since 1.0.0 * @since 4.2.0 The `$autoload` parameter was added. * * @global wpdb $wpdb * * @param string $option Option name. Expected to not be SQL-escaped. * @param mixed $value Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped. * @param string|bool $autoload Optional. Whether to load the option when WordPress starts up. For existing options, * `$autoload` can only be updated using `update_option()` if `$value` is also changed. * Accepts 'yes'|true to enable or 'no'|false to disable. For non-existent options, * the default value is 'yes'. Default null. * @return bool False if value was not updated and true if value was updated. */ function update_option( $option, $value, $autoload = null ) { global $wpdb; $option = trim($option); if ( empty($option) ) return false; wp_protect_special_option( $option ); if ( is_object( $value ) ) $value = clone $value; $value = sanitize_option( $option, $value ); $old_value = get_option( $option ); /** * Filter a specific option before its value is (maybe) serialized and updated. * * The dynamic portion of the hook name, `$option`, refers to the option name. * * @since 2.6.0 * * @param mixed $value The new, unserialized option value. * @param mixed $old_value The old option value. */ $value = apply_filters( 'pre_update_option_' . $option, $value, $old_value ); /** * Filter an option before its value is (maybe) serialized and updated. * * @since 3.9.0 * * @param mixed $value The new, unserialized option value. * @param string $option Name of the option. * @param mixed $old_value The old option value. */ $value = apply_filters( 'pre_update_option', $value, $option, $old_value ); // If the new and old values are the same, no need to update. if ( $value === $old_value ) return false; /** This filter is documented in wp-includes/option.php */ if ( apply_filters( 'default_option_' . $option, false ) === $old_value ) { // Default setting for new options is 'yes'. if ( null === $autoload ) { $autoload = 'yes'; } return add_option( $option, $value, '', $autoload ); } $serialized_value = maybe_serialize( $value ); /** * Fires immediately before an option value is updated. * * @since 2.9.0 * * @param string $option Name of the option to update. * @param mixed $old_value The old option value. * @param mixed $value The new option value. */ do_action( 'update_option', $option, $old_value, $value ); $update_args = array( 'option_value' => $serialized_value, ); if ( null !== $autoload ) { $update_args['autoload'] = ( 'no' === $autoload || false === $autoload ) ? 'no' : 'yes'; } $result = $wpdb->update( $wpdb->options, $update_args, array( 'option_name' => $option ) ); if ( ! $result ) return false; $notoptions = wp_cache_get( 'notoptions', 'options' ); if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) { unset( $notoptions[$option] ); wp_cache_set( 'notoptions', $notoptions, 'options' ); } if ( ! defined( 'WP_INSTALLING' ) ) { $alloptions = wp_load_alloptions(); if ( isset( $alloptions[$option] ) ) { $alloptions[ $option ] = $serialized_value; wp_cache_set( 'alloptions', $alloptions, 'options' ); } else { wp_cache_set( $option, $serialized_value, 'options' ); } } /** * Fires after the value of a specific option has been successfully updated. * * The dynamic portion of the hook name, `$option`, refers to the option name. * * @since 2.0.1 * * @param mixed $old_value The old option value. * @param mixed $value The new option value. */ do_action( "update_option_{$option}", $old_value, $value ); /** * Fires after the value of an option has been successfully updated. * * @since 2.9.0 * * @param string $option Name of the updated option. * @param mixed $old_value The old option value. * @param mixed $value The new option value. */ do_action( 'updated_option', $option, $old_value, $value ); return true; }
更新版本 | 源码位置 | 使用 | 被使用 |
---|---|---|---|
4.2.0 | wp-includes/option.php | 4 | 10 |
笔记(Notes)
根据WordPress Codex(以及我使用WP开发的经验),请注意这里返回值中的一个重要细节:
类别:WordPress 函数手册、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
还没有任何评论,赶紧来占个楼吧!