【问题标题】:Wordpress trying to activate wrong pluginWordpress 试图激活错误的插件
【发布时间】:2014-05-02 12:57:25
【问题描述】:

当我尝试激活我编写的插件时,wordpres 尝试激活错误的插件,我说我去Plugins->Installed Plugins 并激活,wordpress 返回找不到“anspress”插件的错误消息。

我的插件名称“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);

我认为我的插件有问题,但找不到。

【问题讨论】:

标签: php wordpress


【解决方案1】:

在插件的根部,我们应该不惜一切代价避免直接调用 WordPress 函数。最佳做法是将所有内容封装在相关的钩子中。

不过,函数get_option 可以安全使用。有了它,我们处理includes。

对于其余部分,我们使用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 );
}

开始,去掉defines和globalshere's a nice plugin base

【讨论】:

  • 我根据你的链接重写了 academy.php,并且你在这个答案中的模组,它现在运行良好。谢谢
【解决方案2】:

您的插件依赖于另一个名为“anspress”的插件,这就是您收到该错误的原因。您的代码中的这个 sn-p 是问题的根源:

$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”或将其从该数组中删除。

【讨论】:

  • 这个 sn-p 仅用于在 wp admin 上显示警告,如果我安装 'anspress',wordpress 会显示“由于错误,插件 anspress 已停用:插件没有有效的标题。”
猜你喜欢
  • 2014-11-05
  • 2017-11-11
  • 2019-04-27
  • 2016-12-12
  • 2012-07-16
  • 2021-12-31
  • 2022-01-23
  • 2016-02-06
  • 2012-11-19
相关资源
最近更新 更多