I am manipulating an img element in functions.php using jQuery by changing the src attr on hover. How do I make so the absolute path works? Currently, when I view the image directly in the browser it brings me to 404 page. Relative path doesn't work either.

1 Answer 1


i always start every theme w/ some constants

` define('THEME', get_bloginfo('stylesheet_directory')); //or you could use this one: //define('THEME', get_stylesheet_directory_uri());

define('IMAGES', THEME . '/images'); `

and some others, but the point is that at any place in my code i can point to my theme's image folder

img src = "<?php echo IMAGES . '/image.png';?>"

  • Yah that's another way of getting the relative path. But like I said it doesn't work. But if I host the image in another site and use their link, the img displays fine. Maybe it's because of thematic theme? I know I could just switch to another theme to find out but I don't want to break things as of now.
    – jilseego
    Jun 29, 2011 at 6:15
  • doubt it is a thematic issue, i use this w/ thematic all the time. should produce a link like mysite.com/wp-content/themes/mytheme/images/image.png which isn't a relative link. from codex on get_bloginfo 'stylesheet_directory' - Returns the stylesheet directory URL of the active theme. (Was a local path in earlier WordPress versions.) Consider using get_stylesheet_directory_uri() instead. so you could define('THEME', get_stylesheet_directory_uri()); what do you get in your markup (view source) for the image source. that is more telling than "it doesn't work" Jun 29, 2011 at 6:25
  • You are right it works! Maybe I was too sleepy yesterday! :-) Now the problem I am facing is how to do that with jQuery but that's a different question already. Thanks!
    – jilseego
    Jun 29, 2011 at 8:47
  • if you inject your jquery in the header or footer you can still process the php. if you have it in a js file that won't work. >>> var src = "<?php echo IMAGES . '/image.png';?>" Jun 29, 2011 at 14:35

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.