wordpress函数add_option()

add_option( string $option, mixed $value = , string $deprecated = , string|bool $autoload = ‘yes’ )

添加新选项。
Adds a new option.

说明(Description)

不需要序列化值。如果需要序列化该值,则在将其插入数据库之前将对其进行序列化。请记住,资源不能序列化或作为选项添加。

可以创建不带值的选项,然后稍后更新这些值。现有选项将不会更新,并执行检查以确保您没有添加受保护的WordPress选项。应注意不要将选项命名为与受保护选项相同的选项。


参数(Parameters)

参数 类型 必填 说明
$option (string) 必需 要添加的选项的名称。应为非SQL转义。
$value (mixed) 可选 期权价值。如果不是标量,则必须是可序列化的。应为非SQL转义。
$deprecated (string) 可选 描述。不再使用了。
$autoload (string | bool) 可选 WordPress启动时是否加载该选项。默认设置为启用。接受“否”以禁用遗留原因。

返回(Return)

(bool)如果未添加选项,则为False;如果添加选项,则为true。


源码(Source)

/**
 * Add a new option.
 *
 * 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.
 *
 * You can create options without values and then update the values later.
 * Existing options will not be updated and checks are performed to ensure that you
 * aren't adding a protected WordPress option. Care should be taken to not name
 * options the same as the ones which are protected.
 *
 * @since 1.0.0
 *
 * @global wpdb $wpdb
 *
 * @param string         $option      Name of option to add. Expected to not be SQL-escaped.
 * @param mixed          $value       Optional. Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
 * @param string         $deprecated  Optional. Description. Not used anymore.
 * @param string|bool    $autoload    Optional. Whether to load the option when WordPress starts up.
 *                                    Default is enabled. Accepts 'no' to disable for legacy reasons.
 * @return bool False if option was not added and true if option was added.
 */
function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) {
    global $wpdb;
 
    if ( !empty( $deprecated ) )
        _deprecated_argument( __FUNCTION__, '2.3' );
 
    $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 );
 
    // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
    $notoptions = wp_cache_get( 'notoptions', 'options' );
    if ( !is_array( $notoptions ) || !isset( $notoptions[$option] ) )
        /** This filter is documented in wp-includes/option.php */
        if ( apply_filters( 'default_option_' . $option, false ) !== get_option( $option ) )
            return false;
 
    $serialized_value = maybe_serialize( $value );
    $autoload = ( 'no' === $autoload || false === $autoload ) ? 'no' : 'yes';
 
    /**
     * Fires before an option is added.
     *
     * @since 2.9.0
     *
     * @param string $option Name of the option to add.
     * @param mixed  $value  Value of the option.
     */
    do_action( 'add_option', $option, $value );
 
    $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 ) );
    if ( ! $result )
        return false;
 
    if ( ! defined( 'WP_INSTALLING' ) ) {
        if ( 'yes' == $autoload ) {
            $alloptions = wp_load_alloptions();
            $alloptions[ $option ] = $serialized_value;
            wp_cache_set( 'alloptions', $alloptions, 'options' );
        } else {
            wp_cache_set( $option, $serialized_value, 'options' );
        }
    }
 
    // This option exists now
    $notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh
    if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
        unset( $notoptions[$option] );
        wp_cache_set( 'notoptions', $notoptions, 'options' );
    }
 
    /**
     * Fires after a specific option has been added.
     *
     * The dynamic portion of the hook name, `$option`, refers to the option name.
     *
     * @since 2.5.0 As "add_option_{$name}"
     * @since 3.0.0
     *
     * @param string $option Name of the option to add.
     * @param mixed  $value  Value of the option.
     */
    do_action( "add_option_{$option}", $option, $value );
 
    /**
     * Fires after an option has been added.
     *
     * @since 2.9.0
     *
     * @param string $option Name of the added option.
     * @param mixed  $value  Value of the option.
     */
    do_action( 'added_option', $option, $value );
    return true;
}

更新版本 源码位置 使用 被使用
1.0.0 wp-includes/option.php:461 5 17
版权声明:原创作品,未经允许不得转载,否则将追究法律责任。
本站资源有的自互联网收集整理,如果侵犯了您的合法权益,请联系本站我们会及时删除。
本站资源仅供研究、学习交流之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。
本文链接:子佩工作室https://zptheme.com/5476.html
许可协议:《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权

本站分为普通会员,SVIP会员,永久会员。
SVIP会员新用户注册即送180天,到期后可邀请好友2位/180天进行无限续期。
永久会员支持微信支付在线开通。

积分是本站通用虚拟货币,可用于文章资源购买。
每天签到,评论或点赞文章,或者投稿都可免费获得积分
新用户注册免费赠送2积分 邀请用户注册2位/2积分

如果您已经成功付款但是网站没有弹出成功提示,请联系售后提供付款信息为您处理

本站资源属于虚拟商品,具有可复制性,可传播性,一旦授予,不接受任何形式的退款、换货要求。请您在购买获取之前确认好 是您所需要的资源

需要效果图的,可以联系售后(右侧核保售后,扫码添加或者点击 加入我们-联系客服)。或者微信添加企业微信:子佩工作室

计划书请提供费率条款(现价分红),100起。WP主题一般单个问题30起。主题二开500起,开发新主题3000起。低于1000须全款,其余先支付全款的60%,完成后支付余下的。

资源是指寿险计划书(定制除外),WP主题或插件不包含在永久会员之内!

发表回复