I have been using these functions for quite a while now but yesterday I noticed that they were not working properly on a site where the plugin directory was outside the main WordPress folder.

WP_PLUGIN_DIR and WP_PLUGIN_URL have been set properly like so:

define( 'WP_PLUGIN_DIR', '/var/www/plugins' );
define( 'WP_PLUGIN_URL', 'http://test.com/plugins' );

Now if the value of __FILE__ is /var/www/plugins/forms-plugin/forms-plugin.php and I pass it to plugin_basename(), I get this:


Instead of just the plugin name.

If I pass it to plugin_dir_url(), I get:


Instead of the correct value: http://test.com/plugins/forms-plugin/

Everything works properly when the same plugin is placed inside the WP directory and the constants WP_PLUGIN_DIR and WP_PLUGIN_URL are not used.

Am I doing something wrong?

Thanks for your help.

  • I can not reproduce the problem. Working perfectly here. Can you provide exact steps to reproduce the issue? – cybmeta Dec 29 '15 at 12:28
  • "If I pass it to" - if you pass what? – Mark Kaplun Dec 29 '15 at 12:58
  • @MarkKaplun If I pass the FILE variable which has the value /var/www/plugins/forms-plugin/forms-plugin.php – Hasan Akhtar Dec 29 '15 at 14:00
  • @cybmeta All you have to do is move the plugin's directory outside the wordpress directory using the constants WP_PLUGIN_DIR and WP_PLUGIN_URL. Then inside a plugin file use the function plugin_dir_url and pass it the FILE magic constant. – Hasan Akhtar Dec 29 '15 at 14:03
  • As I said, I did it and it worked perfectly here. Following those steps doesn't casue the issue you are describing; that is why I asked you the exact steps to reproduce the problem. It seems that you/we are missing something. – cybmeta Dec 29 '15 at 14:47

Turns out I was adding a trailing slash to WP_PLUGIN_DIR in my wp-config.php which was causing this issue. Hopefully this will help someone making the same mistake.

  • 2
    "Hopefully this will help someone making the same mistake" … hopefully this helps avoiding the mistake. – kaiser Dec 29 '15 at 18:28
  • Please consider accepting your answer so that the question shows as answered. – Andy Macaulay-Brook Jul 23 '16 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.