2014-03-24 110 views
2

當我嘗試激活我寫的一個插件時,wordpres會嘗試激活錯誤的插件,我說我去Plugins->Installed Plugins並激活,wordpress會返回'anspress'插件無法提供的錯誤消息被發現。Wordpress試圖激活錯誤的插件

我的插件名稱爲「Academy」,文件夾名稱爲「academy」,插件文件名爲「academy.php」。

academy.php內容:

<?php 
/** 
* @package Academy 
*/ 
/* 
Plugin Name: Academy 
Description: Academy features 
Version: 0.1 
Author: Victor Aurélio Santos 
Text Domain: academy 
*/ 

/* is_plugin_active */ 
include_once(ABSPATH . 'wp-admin/includes/plugin.php'); 

define ('ACADEMY_INCLUDE', plugin_dir_path(__FILE__) . '/include'); 
define ('ACADEMY_TEMAPLE', ACADEMY_INCLUDE . '/templates'); 

global $academy_err; $academy_err = array(); 

$plugin_dependencies = array('woocommerce' => false, 
         'anspress' => false); 

/* Dependencies check */ 
foreach ($plugin_dependencies as $plugin => &$enabled) { 
    if (! $enabled = is_plugin_active("{$plugin}/{$plugin}.php")) { 
    $academy_err[] = "The {$plugin} plugin isn't installed and/or activated."; 
    } 
} 

/* Check current theme, if not 'Academy' don't load files that depends... */ 
if ("Academy" == wp_get_theme()->Name) { 
    include ACADEMY_INCLUDE . '/buddypress/bp-loader.php'; 
    include ACADEMY_INCLUDE . '/news-feed.php'; 
} else { 
    $academy_err[] = "Current theme isn't \"Academy\" not loading NewsFeed..."; 
} 

/* Check for woocommerce */ 
if ($plugin_dependencies['woocommerce'] && $plugin_dependencies['anspress']){ 
    include ACADEMY_INCLUDE . '/anspress-woocommerce.php'; 
} 

include (ACADEMY_INCLUDE . '/settings.php'); 


function academy_admin_notices() { 
    global $academy_err; 

    foreach ($academy_err as $err) { 
?> 
    <div class="update-nag"> 
     <p>Academy Error: <?php echo $err; ?></p> 
    </div> 
<?php 
    } 
} 
add_action('admin_notices', 'academy_admin_notices'); 

function academy_init() { 
    $plugin_dir = basename(dirname(__FILE__)) . '/languages'; 
    load_plugin_textdomain('academy', false, $plugin_dir); 
} 
add_action('plugins_loaded', 'academy_init'); 

function academy_plugin_action_links ($links, $file) { 
    static $this_plugin; 

    if (!$this_plugin) { 
    $this_plugin = plugin_basename(__FILE__); 
    } 

    if ($file == $this_plugin) { 
    $settings_link = '<a href="' . get_bloginfo('wpurl') . '/wp-admin/admin.php? page=academy-settings">' . __('Settings', 'academy') . '</a>'; 
    array_unshift($links, $settings_link); 
    } 

    return $links; 
} 
add_filter('plugin_action_links', 'academy_plugin_action_links', 10, 2); 

我認爲這是錯誤的東西與我的插件,卻找不到什麼。

+0

is_plugin_active功能 –

+0

@brasofilo法典,http://codex.wordpress.org/Function_Reference/is_plugin_active#Usage –

+0

@brasofilo沒關係,就算我註釋掉這包括使用「is_plugin_active」線, WordPress繼續嘗試啓用錯誤的插件。 –

回答

1

在插件的根源處,我們應該不惜一切代價直接調用WordPress函數。最好的做法是將所有內容都封裝在相關的鉤子中。

但是,功能get_option可以安全使用。有了它,我們處理include s。

其餘的,我們使用register_activation_hook來激活激活插件。要檢查所需插件的激活/停用,我們可以使用鉤子activate_$pluginNamedeactivate_$pluginName。主題狀態更改可以使用鉤子after_switch_theme捕獲。

<?php 
/** 
* Plugin Name: (SO) Academy 
* Plugin URI: http://stackoverflow.com/a/22648487/1287812 
* Description: Activate plugin, check for others, show warnings 
* Version: 0.1b 
* Author: Victor Aurélio Santos, brasofilo 
*/ 

define ('ACADEMY_INCLUDE', plugin_dir_path(__FILE__) . '/include'); 
define ('ACADEMY_TEMAPLE', ACADEMY_INCLUDE . '/templates'); 

$academy_err = get_option('academy_err'); 
if(!isset($academy_err['woocommerce']) && !isset($academy_err['anspress'])) 
{ 
    include ACADEMY_INCLUDE . '/anspress-woocommerce.php'; 
} 
if(!isset($academy_err['theme'])) { 
    include ACADEMY_INCLUDE . '/buddypress/bp-loader.php'; 
    include ACADEMY_INCLUDE . '/news-feed.php'; 
} 
include (ACADEMY_INCLUDE . '/settings.php'); 

register_activation_hook( __FILE__, 'academy_on_activation'); 
add_action('admin_notices', 'academy_admin_notices'); 
add_action('activate_woocommerce/woocommerce.php', 'activate_plugin_woocommerce'); 

function academy_on_activation() 
{ 
    $academy_err = array(); 
    $plugin_dependencies = array( 
     'woocommerce' => 'woocommerce/woocommerce.php', 
     'anspress' => 'anspress/anspress.php' 
    ); 

    /* Dependencies check */ 
    foreach ($plugin_dependencies as $plugin => $file) 
    { 
     if (!is_plugin_active($file)) 
     { 
      $academy_err[$plugin] = "The {$plugin} plugin isn't installed and/or activated."; 
     } 
    } 

    /* Check current theme, if not 'Academy' don't load files that depends... */ 
    if ('Academy' !== wp_get_theme()->Name) 
    { 
     $academy_err['theme'] = "Current theme isn't \"Academy\" not loading NewsFeed..."; 
    } 

    update_option('academy_err', $academy_err); 
} 

function academy_admin_notices() { 
    $academy_err = get_option('academy_err'); 

    foreach ($academy_err as $err) 
    { 
     ?> 
     <div class="update-nag"> 
      <p>Academy Error: <?php echo $err; ?></p> 
     </div> 
     <?php 
    } 
} 

function activate_plugin_woocommerce() 
{ 
    $academy_err = get_option('academy_err'); 
    unset($academy_err['woocommerce']); 
    update_option('academy_err', $academy_err); 
} 

首先和擺脫define S和global小號here's a nice plugin base

+0

我根據你的鏈接重寫了academy.php,並且你在這個答案中使用了mods,現在它工作的很好。謝謝 –

1

你的插件依賴於另一個名爲'anspress',這就是爲什麼你會得到這個錯誤。這從您的代碼段是問題來源:

$plugin_dependencies = array('woocommerce' => false, 
         'anspress' => false); 

/* Dependencies check */ 
foreach ($plugin_dependencies as $plugin => &$enabled) { 
    if (! $enabled = is_plugin_active("{$plugin}/{$plugin}.php")) { 
    $academy_err[] = "The {$plugin} plugin isn't installed and/or activated."; 
    } 
} 

要麼添加插件,「anspress」,這是標記爲依賴或從陣列中刪除。

+0

這段代碼僅用於在wp admin上顯示警告,如果我安裝'anspress',wordpress會顯示「由於錯誤,插件已被停用:插件沒有有效的標題。」 –