Your code isn't working because WordPress deprecated user levels in version 3.0 (June 2010).
What you want to do is create a plugin that upon activation, creates a new role for the owner of the site that is a clone of the administrator role, but cannot edit plugin or theme files. Those capabilities are edit_plugins
and edit_themes
respectively.
Then, upon deactivation, you want to reassign the owners into the administrator role. This way, they can fully regain control of their website by simply deactivating the plugin.
/**
* Plugin Name: WPSE 264483
*/
//* On activation, create an owner role that can do everything but edit themes/plugins
register_activation_hook( __FILE__, 'wpse_264483_activation' );
function wpse_264483_activation() {
$administrator = get_role( 'administrator' );
$owner = clone( $administrator );
unset( $owner->capabilities[ 'edit_plugins' ] );
unset( $owner->capabilities[ 'edit_themes' ] );
add_role( 'owner', 'Owner', $owner->capabilities );
}
//* On deactivation, re-assign all owners to administrators and remove owner role
register_deactivation_hook( __FILE__, 'wpse_264483_activation' );
function wpse_264483_deactivation() {
$owners = get_users( [
'role' = 'owner',
] );
foreach( $owners as $owner ) {
$owner->add_role( 'administrator' );
$owner->remove_role( 'owner' );
}
remove_role( 'owner' );
}
I've enabled a similar plugin for clients upon request, but I think it's a bad idea to do so without their full knowledge and consent. It is their website after all.