返回介绍

is_active_widget()

发布于 2017-09-11 01:09:37 字数 3947 浏览 957 评论 0 收藏 0

is_active_widget( string|false $callback = false,  int|false $widget_id = false,  string|false $id_base = false,  bool $skip_inactive = true )

Whether widget is displayed on the front end.


description

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.


参数

$callback

(string|false) (Optional) Widget callback to check.

Default value: false

$widget_id

(int|false) (Optional) Widget ID. Optional, but needed for checking.

Default value: false

$id_base

(string|false) (Optional) The base ID of a widget created by extending WP_Widget.

Default value: false

$skip_inactive

(bool) (Optional) Whether to check in 'wp_inactive_widgets'.

Default value: true


返回值

(string|false) False if widget is not active or id of sidebar in which the widget is 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;
}

更新日志

Versiondescription
2.2.0Introduced.

相关函数

Uses

  • wp-includes/widgets.php: wp_get_sidebars_widgets()
  • wp-includes/widgets.php: _get_widget_id_base()

Used By

  • wp-admin/includes/widgets.php: wp_list_widgets()
  • wp-includes/widgets/class-wp-widget-recent-comments.php: WP_Widget_Recent_Comments::__construct()
  • wp-includes/class-wp-customize-widgets.php: WP_Customize_Widgets::get_available_widgets()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Only load a script when the widget is active

    
    <?php
    if ( is_active_widget( false, false, $this->id_base, true ) ) {
    	wp_enqueue_script( 'jquery' );
    }
    ?>
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文