I need to get the URL of my theme directory to reference an image in the theme’s image/headers directory. How is this done in PHP?
Leave a Reply
You must be logged in to post a comment.
I need to get the URL of my theme directory to reference an image in the theme’s image/headers directory. How is this done in PHP?
You must be logged in to post a comment.
This function will return the theme directory URL so you can use it in other functions:
Alternatively, this function will echo the theme directory URL to the browser:
So an example for an image in the themes
images/headers
folder would be:What @EAMann said, with a caveat. Eric is right about the general approach and how the functions
bloginfo()
andget_bloginfo()
work and about how to pass the parameter'template_directory'
to get the value you need for (most) themes.However there is a caveat and that caveat is with the newer Child Themes. If you are using a child theme then
'template_directory'
is probably not what you want unless you are actually trying to refer to an image that is in the parent theme directory. Instead for child themes what you probably want is to passstylesheet_directory
(I know, I know, the names don’t tell you what they are but hey, that’s just the way it is!) Borrowing somewhat from Eric’s reply usingstylesheet_directory
would look like this (I shortened the example so it would not wrap):To illustrate the point I wrote a quick standalone file you can drop in your website’s root as
test.php
and run to see what it outputs. First run with a regular theme like TwentyTen then run with a child theme:If you notice things you might notice that there’s a lot more to what you can pass to
bloginfo()
andget_bloginfo()
; study the code and the screenshot below for ideas.Looking at the screenshot you can see that
stylesheet_directory
returns the same thing as'template_directory'
for a regular theme but a different value, and probably the value you need for a child theme.(source: mikeschinkel.com)
By the way, in case you are not familiar with Child Themes where are two other WordPress Answers that might help:
The whole structure of theme builds on top of two options –
template
(holding parent theme folder namre) andstylesheet
(holding child theme folder namr). If there is no child theme used these are the same.To have flexibility of filters, rather than access option directly, there are accordingly
get_template()
andget_stylesheet()
.Now the only thing is missing is to combine those with themes folder location. This can be done with
get_theme_root_uri()
and again conveniently wrapped inget_template_directory_uri()
andget_stylesheet_directory_uri()
.[get_]bloginfo()
withtemplate_directory
orstylesheet_directory
arguments merely wraps these and there is little reason to use it like that. I’d say it is only confusing by having argument saying directory (commonly relates to local paths), but returning URLs.Sumary:
get_template_directory_uri()
to refer to only or parent themeget_stylesheet_directory_uri()
to only or child themeI use this
(dirname(get_bloginfo('stylesheet_url')))