做到这一点有点棘手。RichardM 的答案非常接近,但这是我发现的作品,没有显示致命错误或必须使用wp_die()
. 诀窍是在检查插件的要求register_activation_hook()
后调用。如果您不这样做,那么 WordPress 会愉快地激活插件并生成“已激活插件”。在您检查您的要求之前发送消息。
我也喜欢在我的消息中使用瞬变,这样我就可以随时显示我的错误消息。当您需要与用户交流多条消息时,它会更有用。
示例代码:
<?php
/**
* Plugin Name: My Plugin
* Description: A sample plugin activation call without generating "Plugin activated."
* message if requirements aren't met.
* Author: Slicktrick
* Version: 1.0.0
* Requires at least: 4.0
*/
add_action( 'admin_init', 'check_requirements_for_my_plugin' );
add_action( 'admin_notices', 'show_notices_for_my_plugin' );
function check_requirements_for_my_plugin() {
// Check that we are using PHP 5.1.2 or greater.
// Modify this to use whatever checks you need.
$phpversion = '5.1.2';
if ( version_compare(phpversion(), $phpversion, "<" ) ) {
// deactivate and remove flag to activate plugin
deactivate_plugins( plugin_basename( __FILE__ ) );
if ( isset( $_GET['activate'] ) ) {
unset( $_GET['activate'] );
}
// Create an error message and store it as a transient
// Transient is set to automatically expire after 120 seconds
// This prevents the message from lingering indefinitely
$error_message = "PHP version $phpversion required. My Plugin was not activated.";
set_transient( 'my_plugin_activation_error_message', $error_message, 120 );
} else {
// Here's the trick, we register our activation hook
// now that we know the requirements are met!
register_activation_hook( __FILE__, 'activate_my_plugin' );
}
}
function show_notices_for_my_plugin() {
// We attempt to fetch our transient that we stored earlier.
// If the transient has expired, we'll get back a boolean false
$message = get_transient( 'my_plugin_activation_error_message' );
if ( ! empty( $message ) ) {
echo "<div class='notice notice-error is-dismissible'>
<p>$message</p>
</div>";
}
}
function activate_my_plugin() {
// Add activation code here
}
// Add the rest of your plugin code here
该check_requirements_for_my_plugin()
函数将在每次admin_init
执行时运行。虽然一旦插件被激活,这可能看起来像是一个多余的检查,但想想如果有人更改托管服务提供商或移动到不同的服务器会发生什么。新的服务器环境可能不支持您的插件,如果 WordPress 数据库被直接复制(很可能),用户最终可能会遇到各种错误和功能损坏。check_requirements_for_my_plugin()
所以每次调用我们的函数是必要的。
如果您不想在插件无法激活时向用户显示任何消息(为什么要让用户对插件未激活的原因一无所知???),那么您可以删除以下内容三行代码加上整个show_notices_for_my_plugin()
函数:
// at the top of the file
add_action( 'admin_notices', 'show_notices_for_my_plugin' );
// from the check_requirements_for_my_plugin() function
$error_message = "PHP version $phpversion required. My Plugin was not activated.";
set_transient( 'my_plugin_activation_error_message', $message, 120 );
参考:
- WordPress Codex - 管理员通知
- WordPress Codex - 瞬态 API
更新
根据这个答案和来自 WordPress Lead Developer 的评论,似乎更好的方法是让你的插件保持激活状态,但禁用所有功能。一旦满足要求,您就可以使用其他检查来执行您需要执行的任何安装任务。这可以通过以下示例代码轻松完成:
add_action( 'plugins_loaded', 'check_requirements_for_my_plugin' );
function check_requirements_for_my_plugin() {
// Do whatever checking needed here
if ( ! $requirements_met ) {
add_action( 'admin_notices', 'show_requirements_notice_for_my_plugin' );
return; // Exit your function
}
// Register all other hooks here since you know requirements are
// met if you reach this point
add_action( 'admin_init', 'maybe_update_my_plugin' );
}
function show_requirements_notice_for_my_plugin() {
// I'd expand this to add a list of needed requirements
echo '<div class="notice notice-error">'
. "My-Plugin functionality is disabled because the requirements are not met."
. '</div>';
}
function maybe_update_my_plugin() {
// Check to see if update/installation tasks need to be performed
}
此方法允许您将通知保留在用户面前,以便他们对其采取行动。当他们尝试激活插件时,我的原始答案会显示一条通知,但是一旦加载新页面,通知就会消失,因为此时插件不再处于活动状态。