2012-09-04 81 views
3

我使用下面的代碼在用戶的PHP版本不足時停用插件本身。一個問題是黃色的消息框出現,說明該插件已被激活,儘管它被此功能拒絕。那麼有沒有辦法顯示黃色的信息?禁用激活WordPress插件時出現的消息

function Plugin_Requirements() { 
    global $wp_version; 
    $plugin = plugin_basename(__FILE__); 
    $plugin_data = get_plugin_data(__FILE__, false); 
    $numPHPver='5.1.2';  
    $strMsg .= $plugin_data['Name'] . ': ' . __('requires PHP') . ' ' . $numPHPver . __(' or higher. Your PHP version is : ') . phpversion() . __('. Deactivating the plugin.') . '<br />'; 

    if (version_compare(phpversion(), $numPHPver, "<")) { 
     echo '<div class="error"><p>' . $strMsg . '</p></div>'; 
     deactivate_plugins($plugin); 
    } 
} 
add_action('admin_init', 'Plugin_Requirements'); 
+0

你是否檢查插件是否被禁用bij在WP_OPTIONS表中查找它的值? – Jurgo

+0

不確定你的意思。它用'phpversion()'檢查版本。 – Teno

+0

你在哪裏添加未設置?將它添加到index.php – transilvlad

回答

-2

看來目前不可能將它作爲插件的一部分來完成。

0
function _my_plugin_php_warning() { 
    echo '<div id="message" class="error">'; 
    echo ' <p>My Plugin requires at least PHP 5. Your system is running version ' . PHP_VERSION . ', which is not compatible!</p>'; 
    echo '</div>'; 
} 

function deactivate_plugin_conditional() { 

    if (version_compare(PHP_VERSION, '5.2', '<')) { 
     $plugin = plugin_basename(__FILE__); 

     if (is_plugin_active($plugin)) { 
      deactivate_plugins($plugin);  
     } 
     add_action('admin_notices', '_my_plugin_php_warning'); 

    } else { 
     //Load classes 
    } 
} 

add_action('admin_init', 'deactivate_plugin_conditional'); 

時沒有出錯這隻會激活插件。如果插件已經啓用,它將被禁用。希望這對你有用。請讓我知道結果如何。

+0

'wp_die()'在不同的頁面顯示消息。所以用戶必須回到插件列表頁面。我想只在頁面頂部顯示一個紅色警告框。 – Teno

+0

我不習慣直接操縱數據庫。 'global $ wpdb; \t \t $ myrows = $ wpdb-> get_results(「SELECT * FROM wp_options WHERE option_name ='active_plugins'」); \t \t print_r($ mywors);'什麼也沒說。我希望看到一些有用的例子。 – Teno

+0

我試過你的更新代碼,但它沒有禁用黃色通知框。還需要禁用該插件。 – Teno

6

你可以只取消設置$_GET變量觸發消息:

if (version_compare(phpversion(), $numPHPver, "<")) { 
    echo '<div class="error"><p>' . $strMsg . '</p></div>'; 
    deactivate_plugins($plugin); 
    unset($_GET['activate']); 
} 

我認爲更好的辦法是不要讓你的插件被激活,從退出(或拋出一個錯誤),它是激活鉤。 Wordpress將顯示一條錯誤消息,指出由於致命錯誤而無法激活插件,但在此似乎適用。

function my_activation_hook() { 
    $required_php_version = '5.1.2'; 

    if (version_compare(PHP_VERSION, $required_php_version, '<')) { 
     $plugin_data = get_plugin_data(__FILE__, false); 
     $message = $plugin_data['Name'] . ' ' . __('requires PHP') . ' ' . $required_php_version . __(' or higher. Your PHP version is ') . phpversion() . '.'; 
     echo "<p>{$message}</p>"; 
     exit; 
    } 
} 
register_activation_hook(__FILE__, 'my_activation_hook'); 
+0

有趣。我不想顯示這條消息,「插件無法啓動,因爲它引發了一個致命的錯誤。」可能嗎? – Teno

+0

我試着用'admin_int'鉤子和'register_activation_hook()'來取消設置'$ _GET',但它仍然顯示致命的錯誤信息。我不確定我是否聽到你說的話。你的意思是'unset($ _ GET ['action']);'?但是這個也不會禁用該消息。 – Teno

+1

不,我不認爲你可以隱藏致命的錯誤消息,如果你在激活鉤子退出。 –

0

如果你不想讓消息出現,進入php頁面,並在頁面頂部打開php標籤後立即添加error_reporting(0)。

2

讓這個恰到好處是有點棘手。 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); 

參考文獻:

  1. WordPress Codex - Admin Notices
  2. 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 
} 

這種方法可以讓你保持在通知用戶面前,使他們能夠就此採取行動。我的原始答案會在嘗試激活插件時顯示通知,但是一旦加載新頁面,通知就會消失,因爲插件在此時不再處於活動狀態。

相關問題