函数文档

resume_plugin()

💡 云策文档标注

概述

resume_plugin() 函数用于尝试恢复单个已暂停的插件,通过检查插件是否仍会引发致命错误来确保安全恢复。如果提供了重定向URL,函数会先测试插件文件,避免错误覆盖成功消息。

关键要点

  • 函数接受两个参数:$plugin(必需,要恢复的插件标识符)和 $redirect(可选,重定向URL,默认为空字符串)。
  • 返回值:成功时返回 true,如果插件未被暂停则返回 false,失败时返回 WP_Error 对象。
  • 如果设置了 $redirect,会先通过 plugin_sandbox_scrape() 测试插件是否引发致命错误,避免错误覆盖重定向。
  • 使用 wp_paused_plugins()->delete() 从暂停插件列表中移除插件,以完成恢复操作。

代码示例

function resume_plugin( $plugin, $redirect = '' ) {
    if ( ! empty( $redirect ) ) {
        wp_redirect(
            add_query_arg(
                '_error_nonce',
                wp_create_nonce( 'plugin-resume-error_' . $plugin ),
                $redirect
            )
        );
        ob_start();
        plugin_sandbox_scrape( $plugin );
        ob_clean();
    }
    list( $extension ) = explode( '/', $plugin );
    $result = wp_paused_plugins()->delete( $extension );
    if ( ! $result ) {
        return new WP_Error(
            'could_not_resume_plugin',
            __( 'Could not resume the plugin.' )
        );
    }
    return true;
}

注意事项

  • 函数在 WordPress 5.2.0 版本中引入,用于错误保护机制。
  • 相关函数包括 wp_paused_plugins()、plugin_sandbox_scrape()、wp_redirect() 等,用于处理插件暂停和恢复流程。
  • 确保插件标识符格式正确,通常为 'plugin-folder/plugin-file.php'。

📄 原文内容

Tries to resume a single plugin.

Description

If a redirect was provided, we first ensure the plugin does not throw fatal errors anymore.

The way it works is by setting the redirection to the error before trying to include the plugin file. If the plugin fails, then the redirection will not be overwritten with the success message and the plugin will not be resumed.

Parameters

$pluginstringrequired
Single plugin to resume.
$redirectstringoptional
URL to redirect to. Default empty string.

Return

true|WP_Error True on success, false if $plugin was not paused, WP_Error on failure.

Source

function resume_plugin( $plugin, $redirect = '' ) {
	/*
	 * We'll override this later if the plugin could be resumed without
	 * creating a fatal error.
	 */
	if ( ! empty( $redirect ) ) {
		wp_redirect(
			add_query_arg(
				'_error_nonce',
				wp_create_nonce( 'plugin-resume-error_' . $plugin ),
				$redirect
			)
		);

		// Load the plugin to test whether it throws a fatal error.
		ob_start();
		plugin_sandbox_scrape( $plugin );
		ob_clean();
	}

	list( $extension ) = explode( '/', $plugin );

	$result = wp_paused_plugins()->delete( $extension );

	if ( ! $result ) {
		return new WP_Error(
			'could_not_resume_plugin',
			__( 'Could not resume the plugin.' )
		);
	}

	return true;
}

Changelog

Version Description
5.2.0 Introduced.