Victor Aurélio
Victor Aurélio

Reputation: 2555

WordPress trying to activate wrong plugin

When I try to activate a plugin that I wrote, WordPress tries do activate the wrong plugin, I say I go to Plugins->Installed Plugins and activate, WordPress return the error message that 'anspress' plugin can't be found.

my plugin's name "Academy", folder name "academy", plugin filename "academy.php".

academy.php content:

<?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);

I think that is something wrong with my plugin, but can't find what.

Upvotes: 2

Views: 254

Answers (2)

brasofilo
brasofilo

Reputation: 26055

At the root of a plugin, we should avoid at all costs doing direct calls to WordPress functions. The best practice is encapsulate everything inside relevant hooks.

The function get_option is safe to use, though. With it, we deal with the includes.

For the rest, we use the register_activation_hook to grab the active plugins on activation. To check the activation/deactivation of the required plugins, we can use the hooks activate_$pluginName and deactivate_$pluginName. The theme state changes can be captured with the hook 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 );
}

To start with and get rid of defines and globals, here's a nice plugin base.

Upvotes: 1

Nathan Dawson
Nathan Dawson

Reputation: 19308

You're plugin is dependent on another called 'anspress' which is why you're getting that error. This snippet from your code is where the problem originates:

$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.";
  }
}

Either add the plugin, 'anspress', that's marked as a dependency or remove it from that array.

Upvotes: 1

Related Questions