函数原型
is_active_widget( callable|false $callback = false, string|false $widget_id = false, string|false $id_base = false, bool $skip_inactive = true ): string|false
函数描述
Determines whether a given widget is displayed on the front end.
是否弃用
未弃用
函数参数
-
$callback
callable|false
Optional - Widget callback to check.
Default:
false -
$widget_id
string|false
Optional - Widget ID. Optional, but needed for checking.
Default:
false -
$id_base
string|false
Optional - The base ID of a widget created by extending WP_Widget.
Default:
false -
$skip_inactive
bool
Optional - Whether to check in
'wp_inactive_widgets'.
Default:
true
函数返回值
string|false ID of the sidebar in which the widget is active, false if the widget is not active.
函数位置
File: wp-includes/widgets.php.
函数源码
function is_active_widget( $callback = false, $widget_id = false, $id_base = false, $skip_inactive = true ) {
global $wp_registered_widgets;
$sidebars_widgets = wp_get_sidebars_widgets();
if ( is_array( $sidebars_widgets ) ) {
foreach ( $sidebars_widgets as $sidebar => $widgets ) {
if ( $skip_inactive && ( 'wp_inactive_widgets' === $sidebar || 'orphaned_widgets' === substr( $sidebar, 0, 16 ) ) ) {
continue;
}
if ( is_array( $widgets ) ) {
foreach ( $widgets as $widget ) {
if ( ( $callback && isset( $wp_registered_widgets[ $widget ]['callback'] ) && $wp_registered_widgets[ $widget ]['callback'] === $callback ) || ( $id_base && _get_widget_id_base( $widget ) === $id_base ) ) {
if ( ! $widget_id || $widget_id === $wp_registered_widgets[ $widget ]['id'] ) {
return $sidebar;
}
}
}
}
}
}
return false;
}
源码链接
变更日志
| Version | Description |
|---|---|
| 2.2.0 | Introduced. |

