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