WordPress开发函数add_network_option()

WordPress开发函数add_network_option()

194
2021-03-23 14:50:14

WordPress开发函数add_network_option(),增加一个新的网络选项。

用法:

add_network_option( int $network_id, string $option, mixed $value )

描述:

现有的选项将不会更新。

另请参阅:

add_option()

参数

$network_id

(int) (必需) 网络ID。可以为null默认为当前网络ID。

$option

(string) (必需) 要添加的选项的名称。预计不会被sql转义。

$value

(mixed) (必需) 期权价值,可以是任何东西。预计不会被sql转义。

返回

(bool) 如果添加了该选项,则为True,否则为false。

来源:

文件: wp-includes/option.php

function add_network_option( $network_id, $option, $value ) {

global $wpdb;

if ( $network_id && ! is_numeric( $network_id ) ) {

return false;

}

$network_id = (int) $network_id;

// Fallback to the current network if a network ID is not specified.

if ( ! $network_id ) {

$network_id = get_current_network_id();

}

wp_protect_special_option( $option );

/**

* Filters the value of a specific network option before it is added.

*

* The dynamic portion of the hook name, `$option`, refers to the option name.

*

* @since 2.9.0 As 'pre_add_site_option_' . $key

* @since 3.0.0

* @since 4.4.0 The `$option` parameter was added.

* @since 4.7.0 The `$network_id` parameter was added.

*

* @param mixed $value Value of network option.

* @param string $option Option name.

* @param int $network_id ID of the network.

*/

$value = apply_filters( "pre_add_site_option_{$option}", $value, $option, $network_id );

$notoptions_key = "$network_id:notoptions";

if ( ! is_multisite() ) {

$result = add_option( $option, $value, '', 'no' );

} else {

$cache_key = "$network_id:$option";

// 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_key, 'site-options' );

if ( ! is_array( $notoptions ) || ! isset( $notoptions[ $option ] ) ) {

if ( false !== get_network_option( $network_id, $option, false ) ) {

return false;

}

}

$value = sanitize_option( $option, $value );

$serialized_value = maybe_serialize( $value );

$result = $wpdb->insert(

$wpdb->sitemeta,

array(

'site_id' => $network_id,

'meta_key' => $option,

'meta_value' => $serialized_value,

)

);

if ( ! $result ) {

return false;

}

wp_cache_set( $cache_key, $value, 'site-options' );

// This option exists now.

$notoptions = wp_cache_get( $notoptions_key, 'site-options' ); // Yes, again... we need it to be fresh.

if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {

unset( $notoptions[ $option ] );

wp_cache_set( $notoptions_key, $notoptions, 'site-options' );

}

}

if ( $result ) {

/**

* Fires after a specific network option has been successfully added.

*

* The dynamic portion of the hook name, `$option`, refers to the option name.

*

* @since 2.9.0 As "add_site_option_{$key}"

* @since 3.0.0

* @since 4.7.0 The `$network_id` parameter was added.

*

* @param string $option Name of the network option.

* @param mixed $value Value of the network option.

* @param int $network_id ID of the network.

*/

do_action( "add_site_option_{$option}", $option, $value, $network_id );

/**

* Fires after a network option has been successfully added.

*

* @since 3.0.0

* @since 4.7.0 The `$network_id` parameter was added.

*

* @param string $option Name of the network option.

* @param mixed $value Value of the network option.

* @param int $network_id ID of the network.

*/

do_action( 'add_site_option', $option, $value, $network_id );

return true;

}

return false;

}

更新日志:

WordPress开发函数add_network_option() (https://www.wpmee.com/) WordPress开发教程 第1张

喜欢这个文章就点个赞分享给好友吧~

END

发表评论

一次支付终身使用
免费版本更新
靠谱的技术支持
成为会员