We have tested this PHP snippet and it works correctly. Please make sure you add it using the Code Snippets plugin. Also, please disable third-party plugins and customizations (if there are any) and check if this issue persists. If you use a caching plugin, make sure that caching is disabled for logged-in users.