函数文档

wp_maybe_load_widgets()

💡 云策文档标注

概述

wp_maybe_load_widgets() 函数用于确定是否应加载 Widgets 库,通过检查过滤器并加载默认小部件文件。

关键要点

  • 函数检查 load_default_widgets 过滤器,若返回 false 值则阻止加载 Widgets 库。
  • 默认情况下加载 default-widgets.php 文件,并添加 wp_widgets_add_menu 动作到 _admin_menu 钩子。
  • 适用于 WordPress 2.2.0 及以上版本,开发者可通过过滤器控制 Widgets 库的加载行为。

📄 原文内容

Determines if Widgets library should be loaded.

Description

Checks to make sure that the widgets library hasn’t already been loaded.
If it hasn’t, then it will load the widgets library and run an action hook.

Source

function wp_maybe_load_widgets() {
	/**
	 * Filters whether to load the Widgets library.
	 *
	 * Returning a falsey value from the filter will effectively short-circuit
	 * the Widgets library from loading.
	 *
	 * @since 2.8.0
	 *
	 * @param bool $wp_maybe_load_widgets Whether to load the Widgets library.
	 *                                    Default true.
	 */
	if ( ! apply_filters( 'load_default_widgets', true ) ) {
		return;
	}

	require_once ABSPATH . WPINC . '/default-widgets.php';

	add_action( '_admin_menu', 'wp_widgets_add_menu' );
}

Hooks

apply_filters( ‘load_default_widgets’, bool $wp_maybe_load_widgets )

Filters whether to load the Widgets library.

Changelog

Version Description
2.2.0 Introduced.