Add / get / delete / update_option () does not work when hook (wordpress) is activated

I am currently updating the WordPress plugin, which has been released a long time ago, and part of the “future verification” of the plugin is to get rid of some crappy naming schemes that were implemented in the initial stages of the plugin.

I decided that I would just add an activation hook that would check if any of these names exist, and if so, just delete the plugin parameters array and show a notification allowing the user to know that they need to update their parameters again, because we should have cleared the data for future verification. Sounds easy enough, huh? Apparently not.

I tried all the possible methods that I can think of, and no matter what I do, I CANNOT get the delete_option (), update_option (), add_option () and get_option () functions to work inside the activation hook.

The following is an example of how my activation hook is configured:

// 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' );
+3
source share
1 answer

The following code is a summary of what I created when I needed to update some “crappy naming schemes” in my own plugin :)

I created my solution after requesting this question in WordPress StackExchange. The full thread is worth a 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);

    }
}
+1
source

Source: https://habr.com/ru/post/1745478/


All Articles