This is an extremely basic example. There are a lot of details to cookie management so you may want to do a bit of reading up on cookie paths, domains and expiration.
You also may do better to simply set the cookie with the appropriate hook instead of relying on the user capabilities with the
You can achieve this by
Please be sure to check if only the administrators have the capability to