函数文档

wp_get_active_and_valid_plugins()

💡 云策文档标注

概述

wp_get_active_and_valid_plugins() 函数用于检索当前激活且有效的插件文件路径数组。在 WordPress 升级或安装过程中,该函数不会返回任何插件。

关键要点

  • 返回一个字符串数组,包含相对于插件目录的插件文件路径。
  • 默认插件目录为 wp-content/plugins,可通过在 wp-config.php 中定义 WP_PLUGIN_DIR 和 WP_PLUGIN_URL 来手动更改。
  • 函数会检查插件是否有效,包括验证文件、以 .php 结尾、文件存在,并排除网络插件。
  • 在恢复模式下,会通过 wp_skip_paused_plugins() 过滤掉暂停的插件。
  • 如果启用了 hack_file 选项且存在 my-hacks.php 文件,会将其添加到数组开头并标记为已弃用。

代码示例

function wp_get_active_and_valid_plugins() {
    $plugins        = array();
    $active_plugins = (array) get_option( 'active_plugins', array() );

    // Check for hacks file if the option is enabled.
    if ( get_option( 'hack_file' ) && file_exists( ABSPATH . 'my-hacks.php' ) ) {
        _deprecated_file( 'my-hacks.php', '1.5.0' );
        array_unshift( $plugins, ABSPATH . 'my-hacks.php' );
    }

    if ( empty( $active_plugins ) || wp_installing() ) {
        return $plugins;
    }

    $network_plugins = is_multisite() ? wp_get_active_network_plugins() : false;

    foreach ( $active_plugins as $plugin ) {
        if ( ! validate_file( $plugin )                     // $plugin must validate as file.
            && str_ends_with( $plugin, '.php' )             // $plugin must end with '.php'.
            && file_exists( WP_PLUGIN_DIR . '/' . $plugin ) // $plugin must exist.
            // Not already included as a network plugin.
            && ( ! $network_plugins || ! in_array( WP_PLUGIN_DIR . '/' . $plugin, $network_plugins, true ) )
        ) {
            $plugins[] = WP_PLUGIN_DIR . '/' . $plugin;
        }
    }

    /*
     * Remove plugins from the list of active plugins when we're on an endpoint
     * that should be protected against WSODs and the plugin is paused.
     */
    if ( wp_is_recovery_mode() ) {
        $plugins = wp_skip_paused_plugins( $plugins );
    }

    return $plugins;
}

📄 原文内容

Retrieves an array of active and valid plugin files.

Description

While upgrading or installing WordPress, no plugins are returned.

The default directory is wp-content/plugins. To change the default directory manually, define WP_PLUGIN_DIR and WP_PLUGIN_URL in wp-config.php.

Return

string[] Array of paths to plugin files relative to the plugins directory.

Source

function wp_get_active_and_valid_plugins() {
	$plugins        = array();
	$active_plugins = (array) get_option( 'active_plugins', array() );

	// Check for hacks file if the option is enabled.
	if ( get_option( 'hack_file' ) && file_exists( ABSPATH . 'my-hacks.php' ) ) {
		_deprecated_file( 'my-hacks.php', '1.5.0' );
		array_unshift( $plugins, ABSPATH . 'my-hacks.php' );
	}

	if ( empty( $active_plugins ) || wp_installing() ) {
		return $plugins;
	}

	$network_plugins = is_multisite() ? wp_get_active_network_plugins() : false;

	foreach ( $active_plugins as $plugin ) {
		if ( ! validate_file( $plugin )                     // $plugin must validate as file.
			&& str_ends_with( $plugin, '.php' )             // $plugin must end with '.php'.
			&& file_exists( WP_PLUGIN_DIR . '/' . $plugin ) // $plugin must exist.
			// Not already included as a network plugin.
			&& ( ! $network_plugins || ! in_array( WP_PLUGIN_DIR . '/' . $plugin, $network_plugins, true ) )
		) {
			$plugins[] = WP_PLUGIN_DIR . '/' . $plugin;
		}
	}

	/*
	 * Remove plugins from the list of active plugins when we're on an endpoint
	 * that should be protected against WSODs and the plugin is paused.
	 */
	if ( wp_is_recovery_mode() ) {
		$plugins = wp_skip_paused_plugins( $plugins );
	}

	return $plugins;
}

Changelog

Version Description
3.0.0 Introduced.