One WordPress function I happen to use frequently is get_template_directory_uri(). While I tried to minimize or avoid its use where I can, I still happen to have more than one call to this function in certain pages across my theme. Most of the time in a series of href or inside other functions (for which I normally put the URI resulting from get_template_directory_uri() in a $variable – however I still find that I might have to store again that variable in a different php file).

I wonder, would it be best if I store the returning value (my theme URI) in a global and use echo the global when I need it in different parts of my theme php files?

I’m still learning my way to best practices and uses of variables and globals.

Thank you

2 Answers
2

I would stay stick to the function. PHP caches functions for speed and efficiency. In some situations, using a function is faster then using a variable.

There are other benefits too – imagine if you changed the name of your variable – you would have to go and update every piece of code where it’s used.

Leave a Reply

Your email address will not be published. Required fields are marked *