can_edit_network()
云策文档标注
概述
can_edit_network() 函数用于检查当前页面是否可以编辑指定网络。默认情况下,网络编辑权限仅限于该网络的 Network Admin,但此函数允许通过过滤器覆盖此限制。
关键要点
- 函数接受一个必需的整数参数 $network_id,表示要检查的网络 ID。
- 返回布尔值:如果网络可编辑则返回 true,否则返回 false。
- 默认逻辑基于当前网络 ID 与传入 $network_id 的比较,但可通过 'can_edit_network' 过滤器进行自定义。
- 此函数在 WordPress 3.1.0 版本中引入。
代码示例
function can_edit_network( $network_id ) {
if ( get_current_network_id() === (int) $network_id ) {
$result = true;
} else {
$result = false;
}
/**
* Filters whether this network can be edited from this page.
*
* @since 3.1.0
*
* @param bool $result Whether the network can be edited from this page.
* @param int $network_id The network ID to check.
*/
return apply_filters( 'can_edit_network', $result, $network_id );
}注意事项
- 函数内部使用 get_current_network_id() 获取当前网络 ID,并与传入的 $network_id 进行比较。
- 通过 apply_filters() 应用 'can_edit_network' 过滤器,允许开发者修改返回结果。
- 相关函数包括 get_current_network_id() 和 apply_filters(),用于支持此功能。
- 此函数被 WP_MS_Users_List_Table::column_blogs() 方法使用,处理站点列输出。
原文内容
Determines whether or not this network from this page can be edited.
Description
By default editing of network is restricted to the Network Admin for that $network_id.
This function allows for this to be overridden.
Parameters
$network_idintrequired-
The network ID to check.
Source
function can_edit_network( $network_id ) {
if ( get_current_network_id() === (int) $network_id ) {
$result = true;
} else {
$result = false;
}
/**
* Filters whether this network can be edited from this page.
*
* @since 3.1.0
*
* @param bool $result Whether the network can be edited from this page.
* @param int $network_id The network ID to check.
*/
return apply_filters( 'can_edit_network', $result, $network_id );
}
Hooks
- apply_filters( ‘can_edit_network’, bool $result, int $network_id )
-
Filters whether this network can be edited from this page.
Changelog
| Version | Description |
|---|---|
| 3.1.0 | Introduced. |