is_active_widget( string|false $callback = false, int|false $widget_id = false, string|false $id_base = false, bool $skip_inactive = true )
确定是否在前端显示给定的小部件。
Determines whether a given widget is displayed on the front end.
说明(Description)
$callback或$id_base可以使用,$id_base是扩展WP_Widget类时的第一个参数,不带可选的$Widget_id参数,返回第一个侧边栏的id,其中找到具有给定回调或$id_base的小部件的第一个实例。使用$widget_id参数,返回侧边栏的id,其中包含回调/$id_base和该id的小部件。
注意:$widget_id和$id_base对于单个小部件是相同的。若要生效,此函数必须在小部件初始化后运行,操作为“init”或更高版本。
有关此主题函数和类似主题函数的更多信息,请参阅主题开发人员手册中的条件标记文章。
参数(Parameters)
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
$callback | (string | false) | 可选 | 要检查的小部件回调。 |
$widget_id | (int | false) | 可选 | 小部件ID。可选,但需要检查。 |
$id_base | (string | false) | 可选 | 通过扩展WP_小部件创建的小部件的基本ID。 |
$skip_inactive | (bool) | 可选 | 是否签入“wp_inactive_widgets”。 |
返回(Return)
(string|false)如果小部件未处于活动状态或小部件处于活动状态的侧边栏的id为false。源码(Source)
/**
* Whether widget is displayed on the front-end.
*
* Either $callback or $id_base can be used
* $id_base is the first argument when extending WP_Widget class
* Without the optional $widget_id parameter, returns the ID of the first sidebar
* in which the first instance of the widget with the given callback or $id_base is found.
* With the $widget_id parameter, returns the ID of the sidebar where
* the widget with that callback/$id_base AND that ID is found.
*
* NOTE: $widget_id and $id_base are the same for single widgets. To be effective
* this function has to run after widgets have initialized, at action 'init' or later.
*
* @since 2.2.0
*
* @global array $wp_registered_widgets
*
* @param string $callback Optional, Widget callback to check.
* @param int $widget_id Optional, but needed for checking. Widget ID.
* @param string $id_base Optional, the base ID of a widget created by extending WP_Widget.
* @param bool $skip_inactive Optional, whether to check in 'wp_inactive_widgets'.
* @return string|false False if widget is not active or id of sidebar in which the widget is active.
*/
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;
}
更新版本 | 源码位置 | 使用 | 被使用 |
---|---|---|---|
2.2.0 | wp-includes/widgets.php:866 | 3 | 2 |