My current setup is as follows...

In the main plugin file, these two hooks are set (not inside any class):

register_activation_hook(__FILE__, array('DG_Setup', 'activation'));
register_uninstall_hook(__FILE__, array('DG_Setup', 'uninstall'));

This same main file includes the file containing DG_Setup:

include_once DG_PATH . 'inc/class-setup.php';

And declares $dg_options:

define('DG_OPTION_NAME', 'document_gallery');
$dg_options = get_option(DG_OPTION_NAME);

I assumed that by referencing the main plugin file for the hook, all the setup within would run before my uninstall hook was executed, but this doesn't appear to be the case.

In this same file, a number of declarations and variables are setup. When the uninstall hook fires, it doesn't seem to have access to any of the declared values or to the variable. I have tried explicitly including the main plugin file within the uninstall hook, but that doesn't change anything.

class DG_Setup {
   public static function uninstall() {
      if (!current_user_can('activate_plugins')) return;

      include_once plugin_dir_path(__FILE__) . 'class-thumber.php';

      $options = DG_Thumber::getOptions();
      var_dump($options); // null

      foreach ($options['thumbs'] as $val) {
         if (false !== $val) {


class DG_Thumber {
   public static function getOptions() {
      include_once plugin_dir_path(dirname(__FILE__)) . 'document-gallery.php';

      global $dg_options;
      return $dg_options['thumber'];

Obviously I could just re-declare all of theses values, but then I'm declaring the same value in multiple places which is very un-DRY. So my question is, how can I handle this cleanly?

  • Where is the $dg_options global set? If it is created by a function hooked to an action like 'init', it won't get called. ('init' is happens before the uninstall hook is run.) – J.D. Mar 8 '14 at 19:49
  • @J.D. $dg_options is declared in the main body of the main plugin file, prior to including the file with DG_Setup. – Dan Mar 8 '14 at 19:56

The problem is that you aren't declaring $dg_options as global here:

define('DG_OPTION_NAME', 'document_gallery');
$dg_options = get_option(DG_OPTION_NAME);

During uninstall you aren't in the global scope, your file is being included by uninstall_plugin().

So you need to add this at the top of the main plugin file:

global $dg_options;

Really, any time you want a variable to be global, you should be sure to use the global statement. Don't take scope for granted.

  • That did the trick. Didn't consider that it could end up in a function scope when called. – Dan Mar 9 '14 at 0:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.