Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wordpress plugin development - using images - path not found

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!

like image 914
Matt Facer Avatar asked Jun 16 '10 12:06

Matt Facer


2 Answers

One way to do this is:

plugins_url('images/test.png', __FILE__)

...which will give you the correct URL even if the user has changed the name of the parent directory.

like image 81
colllin Avatar answered Oct 18 '22 15:10

colllin


I used Collin's answer in this way:

    <img src="<?php echo plugins_url( 'images/test.png', __FILE__ ); ?>" border="0" />
like image 36
Stev Avatar answered Oct 18 '22 14:10

Stev