views:

156

answers:

1

I'm writing a plugin for wordpress and am having trouble with images. If I have my plugin in wp-content/plugins/my-plugin/ and in there, a folder images/test.png - how do I reference that image in my code? I don't want to have to put the images in to the theme, as when other users come to get my plugin, the image won't work!

so my structure is

myplugin/plugin.php (which includes several files...)
myplugin/pluginstyle.css
myplugin/includes/page.php
myplugin/images/test.png

I have the style sheet working nicely, but when I try and use an image as the background for an element it doesnt work.

How do I reference the image in my plugin?

test output from page.php

<div class="test"><p>hello</p></div>

css

.test { background: url(../images/test.png) repeat-x; }

where am I going wrong? Is there a method which I should be using? Thanks for any help!

+1  A: 

WordPress' PHP constant WP_PLUGIN_URL contains the absolute URL to your plugins folder. So, to get the url, use WP_PLUGIN_URL . '/myplugin/images/test.png'. In the stylesheet, image paths are always relative to the stylesheet itself. using

.test { background: url(images/test.png); }

should work, as long as it's in an external stylesheet. If it's inline, you should use the absolute URL.

John P Bloch
well, I call the style sheet in my plugin using the wp_head() hook. It simply outputs <link type="text/css" rel="stylesheet" media="all" href="' . get_bloginfo('wpurl') . '/wp-content/plugins/myplugin/pluginstyle.css" />... I don't really want to hard code any style in the PHP files (which is where I'd use the WP_PLUGIN_URL method... or is that the only way? wouldn't images/test.png when called look for that images folder in the root? ie: root/images or what ever the page is (using permalinks) /images ??
Matt Facer
No. With the way you're including the CSS file, using url(images/test.png) will look in the correct folder. Relative URLs inside an external stylesheet reference url paths relative to THE STYLESHEET, not the requested document. This is the exact same way that themes use images in the stylesheet. Same concept.
John P Bloch
yep - just using images/test.png worked.. can't believe it!! Sure I tried that one first... obviously did something wrong!
Matt Facer