Function to allow “Anyone can register”?

You can do it by using this code:

add_action('init', 'update_anyone_can_register');
function update_anyone_can_register() {
  update_option('users_can_register', true);
}

I tested on my functions.php file and it worked as expected.