8

what the best way to include images from the template's images folder? is it by using get_bloginfo('template_url')? I understood that its better to hard code the path to the images folder since this way you save function calls in PHP..

any thoughts?

5 Answers 5

8

The easiest and simplest way to do it is define a unique variable in your theme's functions.php file.

Such as:

<?php
$theme_name_images = get_bloginfo('stylesheet_directory') . '/images/';
?>

No need for classes as a previous answer suggested.

EDIT: It should be get_bloginfo, instead of bloginfo(), as Viper007Bond kindly pointed out.

5
  • 1
    But this can muddy up the global namespace.
    – WhIteSidE
    Aug 12, 2010 at 16:03
  • true, but it's the simplest. I never said it was the cleanest way to do it.
    – john010117
    Aug 12, 2010 at 16:52
  • 1
    Yep but the global namespace muddying is only a problem if he doesn't use a good naming convention for his variables. For example, he could use amits_stylesheet_directory and amits_template_url or instead of "amits" he could use the client's name. Aug 12, 2010 at 16:52
  • 3
    bloginfo() echoes. You want get_bloginfo() which returns. Aug 12, 2010 at 19:37
  • @Viper007Bond - Thanks. I fixed my post.
    – john010117
    Aug 12, 2010 at 22:44
9

I understood that its better to hard code the path to the images folder since this way you save function calls in PHP..

I definitely wouldn't worry about function calls, WordPress' object cache will handle cache all that stuff for you. You could define the path as a constant if you wanted, but I generally wouldn't bother.

I would just do:

<img src="<?php bloginfo('stylesheet_directory')"?>/images/image.png" />

i edited this to

<img src="<?php bloginfo('stylesheet_directory');?>/images/image.png" /> (the above gives errors)

1
  • Just a couple notes: first, I would use get_stylesheet_directory_uri() instead of bloginfo( 'stylesheet_directory' ); second, if the image is not intended to be overridden by a Child Theme, then I would use get_template_directory_uri(). Feb 9, 2012 at 13:48
2

@Amit: It depends. Most function calls are pretty fast in PHP so if you are looking for performance I wouldn't worry about it too much. However I know how you feel seeing all those calls. Still, I'd never want to hardcode paths.

If you are going to be using get_bloginfo('template_url') many times in your theme why not assign it to a global variable in your functions.php file and then reference it everywhere you'd normally reference the function call? Make sure to name it something unlikely to conflict such as amits_template_url. So do this in functions.php:

global $amits_template_url;
$amits_template_url = get_bloginfo('template_url');

And then in your theme reference it everywhere you would have referenced get_bloginfo('template_url').

0

Well, it depends what kind of development you're doing. If you're doing plugin development, you need to use the functions because you'll never know for sure what the url structure will be. If you're doing specific work on a specific website, it's technically ok to hard code it, but that is still inadvisable, since it precludes the possibility that the url structure could ever change.

Whenever I develop plugins or themes, I structure the program in a class; that way, I can save repetitive values as properties of the object (e.g., WP URL and template_url). That way I can use them quickly without calling the functions repeatedly.

1
  • My intention was for specific theme development for a specific client (not a "distributable" theme), can you give a code example for a class like that please?
    – Amit
    Aug 12, 2010 at 15:41
0

Just link your image files from your CSS file, that means you can use the relative path from the CSS file to the images. This is a CSS only solution. No PHP code needed. Work is done by the browser.

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.