Adding a LISTING of PHP code in a page/post [closed]

You can’t put PHP code in a post, without the use of plugins that allow that. You might be able to put a <code></code> tag around the code in a text editor, but that might depend on your theme.

But the code in your question looks like it belongs in the functions.php of your theme (preferably a Child Theme, since you don’t want to change the theme code). When the code is in the functions.php file, it gets executed on every page, and will do what you want. (It’s not clear from your question where you put the code block.)