Free Consultation

Hardcoding Images in a Wordpress Theme Using the bloginfo() Function

Wordpress Logo

If you are creating a Wordpress theme and you need to code in an image, you may notice that you need an absolute path rather than a relative one. If you are confused on the difference between the two:

A relative image path

<img src="images/picture.jpg" alt="Picture" />

An absolute path

<img src="https://www.buckleupstudios.com/wp-content/themes/themeFolder/images/picture.jpg" alt="Picture" />

Wordpress page templates need an absolute link. You can hardcode the whole absolute link, but that gets tedious and can break your code if you ever have to change your file structure. The best bet is to use the bloginfo('template_directory'); function to do it for you.

bloginfo('template_directory'); is a PHP function that inserts the path to your template directory. Therefore, equals "https://www.buckleupstudios.com/wp-content/themes/themeFolder" after PHP does its magic.

So for example, if you code... <img src = '<?php bloginfo('template\_directory'); ?>/images/picture.jpg' alt = 'Picture' /> ...it will dynamically hardcode an image into your Wordpress theme! You can also use this function for linking things like your stylesheet and javascript in the <head> of your file as well.

Check out the Wordpress Codex on bloginfo(); for more great uses.

Lead Generating Websites Starting At $150/month

Questions? Let's talk and see if I'm a good fit for your business.

(Expect a fast reply 😁)