I assume that you want to activate a plugin only when it is necessary. There is a better option to do this. You can activate the whole plugin upon a certain action. I have provided a example code that will help you achieve what you are looking for and if you have any more queries you can put a comment. If it works for you can accept the answer.
function run_activate_plugin( $plugin ) {
$current = get_option( 'active_plugins' );
$plugin = plugin_basename( trim( $plugin ) );
if ( !in_array( $plugin, $current ) ) {
$current[] = $plugin;
sort( $current );
do_action( 'activate_plugin', trim( $plugin ) );
update_option( 'active_plugins', $current );
do_action( 'activate_' . trim( $plugin ) );
do_action( 'activated_plugin', trim( $plugin) );
}
return null;
}
run_activate_plugin( 'akismet/akismet.php' );