activate_plugins

函数
activate_plugins ( $plugins, $redirect = '', $network_wide = false, $silent = false )
参数
  • (string|string[]) $plugins Single plugin or list of plugins to activate.
    Required:
  • (string) $redirect Redirect to page after successful activation.
    Required:
    Default: (empty)
  • (bool) $network_wide Whether to enable the plugin for all sites in the network. Default false.
    Required:
    Default: false
  • (bool) $silent Prevent calling activation hooks. Default false.
    Required:
    Default: false
返回值
  • (bool|WP_Error) True when finished or WP_Error if there were errors during a plugin activation.
定义位置
相关方法
activate_plugindeactivate_pluginsdeactivated_plugins_noticeactivate_sitewide_pluginvalidate_active_plugins
引入
2.6.0
弃用
-

activate_plugins: 这个函数是用来在WordPress中一次激活多个插件的。它只是为每个需要激活的插件调用activation_plugin函数。

激活了多个插件。

当WP_Error返回时,这并不意味着其中一个插件有错误。它意味着一个或多个插件的文件路径是无效的。

一旦其中一个插件出现错误,执行就会停止。

function activate_plugins( $plugins, $redirect = '', $network_wide = false, $silent = false ) {
	if ( ! is_array( $plugins ) ) {
		$plugins = array( $plugins );
	}

	$errors = array();
	foreach ( $plugins as $plugin ) {
		if ( ! empty( $redirect ) ) {
			$redirect = add_query_arg( 'plugin', $plugin, $redirect );
		}
		$result = activate_plugin( $plugin, $redirect, $network_wide, $silent );
		if ( is_wp_error( $result ) ) {
			$errors[ $plugin ] = $result;
		}
	}

	if ( ! empty( $errors ) ) {
		return new WP_Error( 'plugins_invalid', __( 'One of the plugins is invalid.' ), $errors );
	}

	return true;
}

常见问题

FAQs
查看更多 >