How to link to graphic assets within WordPress Plugins

- by

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:


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.



If you enjoy my content, please consider supporting me on Ko-fi. In return you can browse this whole site without any pesky ads! More details here.

Leave a Comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.