Home » Wordpress » add/get/delete/update_option() not working in activation hook (wordpress)

add/get/delete/update_option() not working in activation hook (wordpress)

Posted by: admin November 30, 2017 Leave a comment

Questions:

I’m in the process of updating a WordPress plugin that’s been out for a while, and part of “future proofing” the plugin is to get rid of some shoddy naming schemes that were implemented in the beginning phases of the plugin.

I figured I would simply add an activation hook which would check to see if any of those names existed, and if so, simply delete the plugin options array and display a notice letting the user know they need to update their options again because we had to scrub the data for future proofing. Sounds easy enough, eh? Apparently not.

I’ve tried every possible method I can think of, and no matter what I do, I CANNOT get delete_option(), update_option(), add_option(), and get_option() to work inside the activation hook.

Below is a sample of how my activation hook is setup:

// add activation function to get rid of old options and services
function my_activation_hook() {
  global $opts_array;

  //Set variable to false before running foreach loop
  $needs_update = false;

  //Loop through $opts_array['bookmark'] and check for 'badname-' prefix
  foreach($opts_array['bookmark'] as $bkmrk) {
    if(strpos($bkmrk, 'badname-') !== false) {
      //If any have 'badname-' prefix, set variable to true
      $needs_update = true;
    }
  }
  //If variable is true, delete options and set to default
  if($needs_update === true) {

    //Delete the options
    unset($opts_array);
    delete_option('MyPluginOpts');

    //Reset the array to default values
    $opts_array = array(
      'position' => 'below', // below, above, or manual
      'reloption' => 'nofollow', // 'nofollow', or ''
      'targetopt' => '_blank', // 'blank' or 'self'
      'bgimg-yes' => 'yes', // 'yes' or blank
      'mobile-hide' => '', // 'yes' or blank
      'bgimg' => 'shr', // default bg image
      'shorty' => 'b2l',
      'pageorpost' => '',
      'bookmark' => array_keys($bookmarks_opts_data), // pulled from bookmarks-data.php
      'feed' => '1', // 1 or 0
      'expand' => '1',
      'autocenter' => '1',
      'ybuzzcat' => 'science',
      'ybuzzmed' => 'text',
      'twittcat' => '',
      'tweetconfig' => '${title} - ${short_link}', // Custom configuration of tweet
      'defaulttags' => 'blog', // Random word to prevent the Twittley default tag warning
      'warn-choice' => '',
      'doNotIncludeJQuery' => '',
      'custom-mods' => '',
      'scriptInFooter' => '1',
      'vernum' => 'old', //Set to "old" to trigger update notice
    );
    add_option('MyPluginOpts', $opts_array); // Store the option to the database wp_options table in a serialized array
    $opts_array = get_option('MyPluginOpts');// Now reload the variable with stored options from database
  }
}
register_activation_hook(__FILE__, 'my_activation_hook' );
Answers:

The following code is a summary of what I built when I needed to update some “shoddy naming schemes” in my own plugin 🙂

I’ve built my solution after asking this question in WordPress StackExchange. The full thread is a worth read.

class MyPlugin {
    var $adminOptionsName = "MyPlugin";

    function MyPlugin() {
    }

    function init() {
        $this->getAdminOptions();
    }

    function getAdminOptions() {

        // New options and values
        $theNewOptions = array(
            'option_1' => 0,
            'option_2' => '',
            'version'  => '1.0'
        );

        // Grab the options in the database
        $theOptions = get_option($this->adminOptionsName);

        // Check if options need update
        if( !isset($theOptions['version']) && !empty($theOptions) ) {
            foreach( $theOptions as $key => $value ) {
                if( $key == 'not_needed' ) {
                    unset( $theOptions[$key] );
                }
                if( $key == 'old_option_1') {
                    $theOptions['option_1'] = $value;
                    unset( $theOptions[$key] );
                }
                // etc...
            }
        }

        // Proceed to the normal Options check
        if (!empty($theOptions)) {
            foreach ($theOptions as $key => $option) {
                $theNewOptions[$key] = $option;
            }
        }

        update_option($this->adminOptionsName, $theNewOptions);

    }
}