0

I have 2 functions that are very similar, one for the plugin upgrade process, one for the plugin activate process - I have the both so if the user either updates using the WordPress dashboard or by deleting the plugin completely or overwriting, the functions will fire. They work fine and I call the file containing them using a conditional so they only ever run for anyone upgrading from an earlier version of my plugin.

My question is: How can I make these more efficient?

They work, but I get the feeling that there is no need to repeat any of this code and the content of each function is the same.

/**
 * Update plugin settings on activate
 */
function prefix_plugin_activate() {

    $new_option         = array( 'new_setting' => 'on' );
    $existing_settings  = get_option( 'existing_settings' );
    $new_settings       = array_merge( $new_option, $existing_settings );

    update_option( 'existing_settings', $new_settings );

}
// register_activation_hook( __FILE__, 'prefix_plugin_activate' );
register_activation_hook( PREFIX_PLUGIN_PATH . 'main-plugin-file.php', 'prefix_plugin_activate' );

/**
 * Update plugin settings on upgrade
 */
function prefix_plugin_upgrade( $upgrader_object, $options ) {

    $new_option         = array( 'new_setting' => 'on' );
    $existing_settings  = get_option( 'existing_settings' );
    $new_settings       = array_merge( $new_option, $existing_settings );

    update_option( 'existing_settings', $new_settings );

}
add_action( 'upgrader_process_complete', 'prefix_plugin_upgrade',10, 2);
0

You've passed those $upgrader_object and $options parameters to prefix_plugin_upgrade function. But you're not actually using those parameters inside the function. So you can merge those functions like below-

/**
 * Update plugin settings on activate
 * Update plugin settings on upgrade
 */
function prefix_plugin_activate() {

    $new_option         = array( 'new_setting' => 'on' );
    $existing_settings  = get_option( 'existing_settings' );
    $new_settings       = array_merge( $new_option, $existing_settings );

    update_option( 'existing_settings', $new_settings );

}
// register_activation_hook( __FILE__, 'prefix_plugin_activate' );
register_activation_hook( PREFIX_PLUGIN_PATH . 'main-plugin-file.php', 'prefix_plugin_activate' );
add_action( 'upgrader_process_complete', 'prefix_plugin_activate', 10, 2);

Hope this is gonna help.

  • Thanks! I didn't think of that. It works on plugin activation, but I can't figure out how to test the WordPress upgrade process to make sure the add_action( 'upgrader_process_complete', 'prefix_plugin_activate', 10, 2); is fired. – Benbodhi Aug 19 '16 at 19:22
  • If the code snippet from your question work well then it will also work well. Cause we are just binding the function to the hook where as in your code they were two different function. So I'm pretty sure if your question code works then my suggestion will must work. @BenbodhiMantra – CodeMascot Aug 19 '16 at 19:57
  • That's what I'm hoping, but would certainly like to test the admin dashboard update process before submitting the new version to the repo. Neither of them fire if the user updates the files via FTP without deactivating the plugin and reactivating, which is another issue. – Benbodhi Aug 19 '16 at 20:02
  • If this answer helps you, can you please consider it as accepted ? @BenbodhiMantra – CodeMascot Aug 19 '16 at 20:12

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.