add_network_option( int $network_id, string $option, mixed $value )
Add a new network option.
描述
Existing options will not be updated.
参数
- $network_id
-
(int)
(Required)
ID of the network. Can be null to default to the current network ID. - $option
-
(string)
(Required)
Name of option to add. Expected to not be SQL-escaped. - $value
-
(mixed)
(Required)
Option value, can be anything. Expected to not be SQL-escaped.
返回值
(bool) False if option was not added and true if option was added.
源代码
File: 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;
}
更新日志
Version | 描述 |
---|---|
4.4.0 | Introduced. |
相关函数
Uses
-
wp-includes/load.php:
get_current_network_id() -
wp-includes/option.php:
get_network_option() -
wp-includes/cache.php:
wp_cache_get() -
wp-includes/cache.php:
wp_cache_set() -
wp-includes/formatting.php:
sanitize_option() -
wp-includes/load.php:
is_multisite() -
wp-includes/functions.php:
maybe_serialize() -
wp-includes/plugin.php:
apply_filters() -
wp-includes/plugin.php:
do_action() -
wp-includes/option.php:
pre_add_site_option_{$option} -
wp-includes/option.php:
add_site_option_{$option} -
wp-includes/option.php:
add_site_option -
wp-includes/option.php:
add_option() -
wp-includes/option.php:
wp_protect_special_option() -
wp-includes/wp-db.php:
wpdb::insert()
Show 10 more uses
Hide more uses
Used By
-
wp-includes/option.php:
update_network_option() -
wp-includes/option.php:
add_site_option()
User Contributed Notes
你可能对这些文章感兴趣:
- wordpress函数floated_admin_avatar()用法示例
- wordpress函数category_description()用法示例
- wordpress函数attachment_submitbox_metadata()用法示例
- wordpress函数attachment_id3_data_meta_box()用法示例
- wordpress函数atom_site_icon()用法示例
- wordpress函数atom_enclosure()用法示例
- wordpress函数array_replace_recursive()用法示例
- wordpress函数apply_filters_ref_array()用法示例
- wordpress函数apply_filters_deprecated()用法示例
- wordpress函数apache_mod_loaded()用法示例
- wordpress函数antispambot()用法示例
- wordpress函数allow_subdomain_install()用法示例
- wordpress函数allow_subdirectory_install()用法示例
- wordpress函数allowed_tags()用法示例
如有疑问,请前往问答中心反馈!
反馈