我也喜歡爲我的消息使用瞬變,所以我可以隨時顯示我的錯誤消息。當您需要與用戶溝通的多條消息時,它會更加有用。
示例代碼:執行
<?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 - Admin Notices
- WordPress Codex - Transients API
更新
基於 這個answer和這個comment來自一個WordPress主要開發者,它似乎更好的路線是讓你的插件激活,但禁用所有功能。一旦符合要求,您就可以使用其他檢查來執行您需要執行的任何安裝任務。這可以用下面的示例代碼很容易做到:
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
}
這種方法可以讓你保持在通知用戶面前,使他們能夠就此採取行動。我的原始答案會在嘗試激活插件時顯示通知,但是一旦加載新頁面,通知就會消失,因爲插件在此時不再處於活動狀態。
你是否檢查插件是否被禁用bij在WP_OPTIONS表中查找它的值? – Jurgo
不確定你的意思。它用'phpversion()'檢查版本。 – Teno
你在哪裏添加未設置?將它添加到index.php – transilvlad