1

Can I please ask you about the performance of these two approaches in term of execution speed and server load?

approach 1:

<img src="<?php echo get_bloginfo('template_directory'); ?>/data1/images/1.jpg">
<img src="<?php echo get_bloginfo('template_directory'); ?>/data1/images/2.jpg" />
<img src="<?php echo get_bloginfo('template_directory'); ?>/data1/images/3.jpg" />

approach 2:

<?php $variable= get_bloginfo('template_directory'); ?>
<img src="<?php echo $variable; ?>/data1/images/1.jpg">
<img src="<?php echo  $variable; ?>/data1/images/2.jpg" />
<img src="<?php echo $variable; ?>/data1/images/3.jpg" />

The answer to this question will be very useful for me since I meet such cases many times during Wordpress developement. Is the time to get variable content less than the time querying the database for blog info?

3

There is no performance difference, because the result of get_bloginfo() comes from an internal cache anyway, because most (all?) of the return values come from get_option() calls, and these are cached internally with wp_cache_set() and fetched with wp_cache_get(). See Exploring the WordPress Cache API.

Even if there would be a difference it would be too small to be relevant.

The more important difference is readability. This is easier to read and less error prone:

$template_dir = get_template_directory_uri(); 

foreach ( array ( 1, 2, 3 ) as $n )
    echo "<img src='$template_dir/data1/images/$n.jpg' alt=''>";
  • Thank you for your answer. let me please ask you a question. Could you please give me some additional info about this internal cache? is it server side or client side? what I know is that caching may depend on server installed caching systems (like memechached for example), reverse proxies, or headers responses obliging the browser to cache. Does Wordpress force browser to cache? – whiteletters in blankpapers Jul 20 '13 at 22:54
  • @whitelettersandblankspaces I made an update to my answer. The internal cache exists for the current request only, it is limited, but good enough for your use case. – fuxia Jul 20 '13 at 22:58
  • It is the first time I hear about wp_cache_set(), I am reading about it, and frankly, it will be highly appreciated if I have a link talking about this (limited) cache and alternatives. the get_bloginfo function was only a simple exmple to trigger big learning about wordpress performance and caching in the case of high concurrency websites. Thanks agains – whiteletters in blankpapers Jul 20 '13 at 23:03
  • 1
    I have added a useful link. :) – fuxia Jul 20 '13 at 23:08
  • 1
    get_bloginfo('template_directory') is just a wrapper for the real function. – fuxia Jul 21 '13 at 1:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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