函数文档

wp_find_widgets_sidebar()

💡 云策文档标注

概述

wp_find_widgets_sidebar() 函数用于查找指定小工具所属的侧边栏。它通过遍历所有侧边栏及其小工具实例ID,返回匹配的侧边栏ID或null。

关键要点

  • 参数:$widget_id(字符串,必需),要查找的小工具ID。
  • 返回值:字符串或null,找到的侧边栏ID,未找到时返回null。
  • 内部使用 wp_get_sidebars_widgets() 获取侧边栏和小工具列表。
  • 在 WordPress 5.8.0 版本中引入。

代码示例

function wp_find_widgets_sidebar( $widget_id ) {
    foreach ( wp_get_sidebars_widgets() as $sidebar_id => $widget_ids ) {
        foreach ( $widget_ids as $maybe_widget_id ) {
            if ( $maybe_widget_id === $widget_id ) {
                return (string) $sidebar_id;
            }
        }
    }

    return null;
}

注意事项

  • 函数名、参数和返回值中的技术术语如 wp_get_sidebars_widgets() 和 null 不翻译。
  • 相关用途包括 WP_REST_Widgets_Controller 中的多个方法,用于权限检查、获取、更新和删除小工具。

📄 原文内容

Finds the sidebar that a given widget belongs to.

Parameters

$widget_idstringrequired
The widget ID to look for.

Return

string|null The found sidebar’s ID, or null if it was not found.

Source

function wp_find_widgets_sidebar( $widget_id ) {
	foreach ( wp_get_sidebars_widgets() as $sidebar_id => $widget_ids ) {
		foreach ( $widget_ids as $maybe_widget_id ) {
			if ( $maybe_widget_id === $widget_id ) {
				return (string) $sidebar_id;
			}
		}
	}

	return null;
}

Changelog

Version Description
5.8.0 Introduced.