How to link to graphic assets within WordPress Plugins

When you want to display graphics that reside inside your own plugin directory you can serve those via a handy built-in WordPress function called plugins_url. It works just like its counterpart get_stylesheet_directory_uri does in themes.

For example, if you’d like to link to a file that resides at

http://yourdomain.com/wp-content/plugins/your-plugin/images/your-graphic.png

then you can write this URL out like so:

<?php echo plugins_url('images/your-graphic.png', __FILE__); ?>

Note the usage of the PHP constant __FILE__ (that’s two underscores before and after FILE), it’s called a Magic Constant. In this case it points to your own plugin’s directory. If you leave this parameter out you’ll get the URL to the main plugins directory.

If instead you’d like to access the full server path to this file rather than its URL, you can use the plugin_dir_path function.





Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.