Get plugin_dir_url() from one level deep within plugin

I’ve written several plugins using the structure :

/plugins/myplugin/myplugin.php
/plugins/myplugin/class/class-myclass.php

So as to take advantage of OO and overall structuring my code

From within the class file there are times I need to get the URL of the base plugin… I have been using the following, but I’m sure there’s a better way:

$this->plugin_location = substr(plugin_dir_url(__FILE__),0, strrpos(plugin_dir_url(__FILE__), "https://wordpress.stackexchange.com/",-2)) . "https://wordpress.stackexchange.com/";

Another idea I toyed with was having an additional singleton class that stores all of the settings for the plugin and adding an abstraction layer via the class files.

Any help is greatly appreciated.

2

In a subdirectory within your plugin directory you can use the following code:

$this->plugin_location = plugin_dir_url(dirname(__FILE__));

Leave a Comment