0

Is there any benefit perfomance wise when defining a constant in the wp-config.php like so

<php define('PATH_TO_THEME', get_stylesheet_directory_uri() ); ?>

and then referring to PATH_TO_THEME instead using get_stylesheet_directory_uri() multiple times?

I got this from a wordpress handbook, saying that when you call this function multiple times it consumes more performance in comparison to using a constant like PATH_TO_THEME. Is that true? And why?

Or does it only give a performance boost when using an absolute uri to define the constant like so <php define('PATH_TO_THEME', 'http://example.com/wp-content/themes/themename' ); ?> This of course would mean you had to change the uri anytime the url of the website or the location of the stylesheet directory changed.

2 Answers 2

2

You can read up the source code of theme.php where get_stylesheet_directory_uri() is defined, inside you'll see a str_replace() wich is consuming additional resources on each request. The best way to avoid this is to define a variable or constant that contains the path. In that case it is only called once.

https://core.trac.wordpress.org/browser/tags/4.3.1/src/wp-includes/theme.php#L0

2

While calling get_stylesheet_directory_uri multiple times does consume some extra resources versus setting a constant, I doubt you could reliably measure the overall impact it would have on your code. You would also lose the filter applied to the output of get_stylesheet_directory_uri, if it was changed depending on context, and make your code slightly less readable.

If you're trying to eek out some extra speed on your site, this would probably be at the bottom of the list of optimizations.

2
  • I can't think of a specific case where I would apply a filter to get_stylesheet_directory_uri since I only used it to link my stylesheets but I guess you have a point - probably not worth it. Would be interesting though how much of a speed improvement one could get with this tweak when the function is being called a lot.
    – MrKainig
    Nov 16, 2015 at 22:23
  • define "a lot". calling it 100,000 times would have a measurable impact. calling it 20 times in the context of everything else WordPress is doing, you wouldn't be able to detect a difference.
    – Milo
    Nov 16, 2015 at 22:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.