How to link to images in my plugin regardless of the plugin folder’s name

What is the correct method to refer to images from within your plugin code, so that no matter what the folder is named, they resolve correctly?

I had an issue where a user downloaded my plugin twice, then used the 2nd downloaded file to install it. Since Windows automatically named duplicate files foldername(2).zip, when my plugin was uploaded to the site, it was placed in a folder named “foldername(2). So since my images were linked to wp-content/plugins/foldername/img/foo.png, none of the images were showing up.

4 Answers
4

I am not sure about this specific example but in general plugins_url() is fitting function for this.

Example from Codex:

plugins_url('/images/wordpress.png', __FILE__);

Leave a Comment