How to link to graphic assets within WordPress Themes

When you’re displaying graphics that are served from your own theme directory, hard coding is never a good idea. In fact it’s impossible because you don’t know your user’s URL.

There are a couple of built-in WordPress functions that can help us here. One of them is called get_stylesheet_directory_uri which returns the URL to your theme or child theme.

Here’s an example. Say you’d like to display the following file:

then you’d write this in PHP like so:

This will work for both themes and child themes. If you are using a child theme and you’d like to explicitly call a file in the parent theme’s directory you can use get_template_directory_uri like so:

Those options are great if you need the link, but what if you’re writing some PHP code and need the full server path to those files instead? Fear not, WordPress has functions for those situations as well – and they’re called the same thing, just without the _uri at the end (get_stylesheet_directory and get_template_directory respectively).

About Jay Versluis

Jay is a medical miracle known as a Super Survivor. He runs two YouTube channels, five websites and several podcast feeds. To see what else he's up to, and to support him on his mission to make the world a better place, check out his Patreon Campaign.

2 thoughts on “How to link to graphic assets within WordPress Themes

Add your voice!