How to setup admin redirect based on membership level on main site?

I am trying to setup a network function (multisite) that will check the user level (via the Membership plugin) and redirect if it is not that level. I have a basis for the code via I tried replacing $cap with $level_id and updating the level in the code with the correct ID but it still comes back with undefined variable for $level_id. Please let me know how I can modify the code from the plugin on to make this happen. Thank you.