get_network()
云策文档标注
概述
get_network() 函数用于根据网络 ID 或网络对象检索网络数据,数据会经过缓存和过滤器处理。如果未提供网络参数,则使用当前全局网络。
关键要点
- 参数 $network 可选,可以是 WP_Network 对象、整数 ID 或 null,默认为 null 表示使用当前网络。
- 返回值是 WP_Network 对象或 null(如果未找到网络)。
- 函数内部通过 WP_Network::get_instance() 或构造函数处理不同输入类型。
- 检索后触发 get_network 过滤器,允许修改网络数据。
代码示例
function get_network( $network = null ) {
global $current_site;
if ( empty( $network ) && isset( $current_site ) ) {
$network = $current_site;
}
if ( $network instanceof WP_Network ) {
$_network = $network;
} elseif ( is_object( $network ) ) {
$_network = new WP_Network( $network );
} else {
$_network = WP_Network::get_instance( $network );
}
if ( ! $_network ) {
return null;
}
$_network = apply_filters( 'get_network', $_network );
return $_network;
}注意事项
- 函数自 WordPress 4.6.0 版本引入。
- 相关函数包括 WP_Network::__construct()、WP_Network::get_instance() 和 apply_filters()。
- 在多个多站点功能中被使用,如网络管理、站点初始化和 URL 处理等。
原文内容
Retrieves network data given a network ID or network object.
Description
Network data will be cached and returned after being passed through a filter.
If the provided network is empty, the current network global will be used.
Parameters
$networkWP_Network|int|nulloptional-
Network to retrieve. Default is the current network.
Default:
null
Source
function get_network( $network = null ) {
global $current_site;
if ( empty( $network ) && isset( $current_site ) ) {
$network = $current_site;
}
if ( $network instanceof WP_Network ) {
$_network = $network;
} elseif ( is_object( $network ) ) {
$_network = new WP_Network( $network );
} else {
$_network = WP_Network::get_instance( $network );
}
if ( ! $_network ) {
return null;
}
/**
* Fires after a network is retrieved.
*
* @since 4.6.0
*
* @param WP_Network $_network Network data.
*/
$_network = apply_filters( 'get_network', $_network );
return $_network;
}
Hooks
- apply_filters( ‘get_network’, WP_Network $_network )
-
Fires after a network is retrieved.
Changelog
| Version | Description |
|---|---|
| 4.6.0 | Introduced. |