wp_unregister_ability()
云策文档标注
概述
wp_unregister_ability() 函数用于从 Abilities API 中注销一个已注册的能力。它可以在能力注册后的任何时间调用,以禁用其他插件提供的能力或移除不再需要的能力。
关键要点
- 函数作用:从全局注册表中移除一个先前注册的能力。
- 参数:$name(字符串,必需),指定要注销的能力名称,需包含命名空间前缀(例如 'my-plugin/my-ability')。
- 返回值:成功时返回注销的 WP_Ability 实例,失败时返回 null。
- 相关函数:wp_has_ability() 用于检查能力是否已注册,wp_register_ability() 用于注册能力。
- 内部实现:通过 WP_Abilities_Registry::get_instance() 获取注册表实例,并调用其 unregister() 方法。
代码示例
if ( wp_has_ability( 'other-plugin/some-ability' ) ) {
wp_unregister_ability( 'other-plugin/some-ability' );
}注意事项
- 确保在注销能力前使用 wp_has_ability() 检查其是否存在,以避免错误。
- 此函数在 WordPress 6.9.0 版本中引入。
原文内容
Unregisters an ability from the Abilities API.
Description
Removes a previously registered ability from the global registry. Use this to disable abilities provided by other plugins or when an ability is no longer needed.
Can be called at any time after the ability has been registered.
Example:
if ( wp_has_ability( 'other-plugin/some-ability' ) ) {
wp_unregister_ability( 'other-plugin/some-ability' );
}
See also
Parameters
$namestringrequired-
The name of the ability to unregister, including namespace prefix (e.g.,
'my-plugin/my-ability').
Source
function wp_unregister_ability( string $name ): ?WP_Ability {
$registry = WP_Abilities_Registry::get_instance();
if ( null === $registry ) {
return null;
}
return $registry->unregister( $name );
}
Changelog
| Version | Description |
|---|---|
| 6.9.0 | Introduced. |