The code does not contain an error. If you're editing your functions.php inside WordPress, that might be the case as WordPress does not allow or is strict in accepting codes in the backend. You should edit your functions.php directly.
Please also note that we do not have support for custom code issues and enhancments. Please hire a third party developer to implement the code correctly for you.
Thank you for understanding.