Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 275 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Wordpress get plugin directory

#11
The other solutions either won't work if called from a sub-directory of your plugin or they require that you to hard code your plugin's directory name or you need to define a global variable.

My solution uses a function that can be placed in any directory of your plugin without requiring that you to hard code something.

function get_my_plugin_root_dir()
{
// WP_PLUGIN_DIR will have something like "/home/michael/public_html/wp-content/plugins"
// __DIR__ will have something like "/home/michael/public_html/wp-content/plugins/my-plugin-folder/the-dir-being-called-from

// First, we remove WP's root plugin path from our __DIR__ path.
$path_to_this_files_dir_without_wp_plugin_dir = str_replace(WP_PLUGIN_DIR, '', __DIR__);

// Now, we're left with only our plugin's path, which looks like
// this: "/my-plugin-folder/the-dir-being-called-from"

// Next, we discard any sub-directories that are included with our
// plugin's path and keep the first directory in the path, our
// plugin's directory name.
$plugin_directory_name_only = explode(DIRECTORY_SEPARATOR, $path_to_this_files_dir_without_wp_plugin_dir)[1];

// Now $plugin_directory_name_only has "my-plugin-folder"

// Lastly, we build the plugin's complete root path using
// WP's root plugin directory and our plugin's root dir name.
return WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $plugin_directory_name_only;
}

Reply

#12
`plugin_dir_path( __FILE__ )` will give you plugin **path of current file**.

this is mean if you call this function like that inside **"your_plugin_dir/sub_dir/file.php"**
will return **"your_plugin_dir/sub_dir/"**

if you want to get the **ROOT of your plugin directory**, just change `__FILE__` to `__DIR__`

plugin_dir_path( __DIR__ )

Reply

#13
As of now, there isn't any function that will return only the full path of your plugin's base directory.

However, the constant `WP_CONTENT_DIR` is used by WordPress for just about that. But, even though, WordPress uses this constant, they warn us not to use it directly in our plugins or themes. That's because it can cause issues like when users change or rename their plugin's directory from the default one.

Using the `plugin_dir_path()` function will return the path of the file's directory from where it is called. So, if you call this function from a PHP file that is inside a subfolder of your plugin, it will return the path of that subfolder's directory and not your plugin's base directory.


# Answer #


Now, as a workaround, we can define a constant in a PHP file that is directly inside the plugin's base directory and then set its value to the result of calling `plugin_dir_path( __FILE__ )`. This way, we will be able to use that constant anywhere in the plugin to get the full path of the plugin's base directory.

So, open a PHP file located directly in your plugin's base directory. For instance:

../wp-content/plugins/myplugin/myplugin.php

Then, define a constant by setting its value to the result of calling `plugin_dir_path()` function. For instance:

define( 'MYPLUGIN_PLUGIN_DIR_PATH', plugin_dir_path( __FILE__ ) );

This constant now holds the path of your plugin's root directory. So, now, if you echo that constant from any file located in any directory of your plugin like this:

echo MYPLUGIN_PLUGIN_DIR_PATH;

It will print something like this:

/home/example.com/public_html/wp-content/plugins/myplugin/
Reply

#14
You can define a constant into your main PHP file. It should be at the root of your plugin folder.
The file should be here : `.../wp-content/plugins/plugin-folder/my-plugin.php`

You can add into the file this line.

define( 'MYPLUGIN__PLUGIN_DIR_PATH', plugins_url( '', __FILE__ ) );

After you can use your new constant anywhere in your plugin.

public function Test()
{
$folder2 = MYPLUGIN__PLUGIN_DIR_PATH . '/folder1/folder2/';
// $folder2 = .../wp-content/plugins/plugin-folder/folder1/folder2/
}

I hope it will help someone.
Reply

#15
This solution wasn't available when the original answer was accepted.
If the current file is two folders down into the plugin path, use this:
```php
$plugin_path = plugin_dir_path(dirname(__FILE__, 2));
$plugin_url = plugin_dir_url(dirname(__FILE__, 2));
```
Note the number as the second parameter on [dirname()][1], available since PHP v7.0.0.

The technique to use a constant works with this too, this time outside of the root-level plugin.php :
```php
// from pluginname/src/API/Common/Constants.php
define( 'MYPLUGIN_PLUGIN_DIR_PATH', plugin_dir_path(dirname(__FILE__, 3)) );
```

Note [this comment][2] on the PHP Manual site:
> Dirname likes to mess with the slashes.

So I normalize the path to avoid double slashes, and slashes that are wrong for the current OS:

```php
$normalized = wp_normalize_path( $plugin_path . '/' . $required_file);
if (file_exists($normalized))
require_once $normalized;
else
die(__('Required file does not exist') . ": $normalized");
```


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#16
Kinda late to this party, but just in case some else stumbles upon this.

`plugin_dir_path(__FILE__)` will always return the current path (where the file calling it is located).
If you want the root, use the code below:

plugin_dir_path( dirname( __FILE__ ) )

You can then define a constant:

define( 'YOUR_PLUGIN_DIR', plugin_dir_path( dirname( __FILE__ ) ) );
require_once YOUR_PLUGIN_DIR . 'includes/admin-page.php'
require_once YOUR_PLUGIN_DIR . 'admin/classes.php'
Reply

#17
I would suggest to use a WordPress internal constant to solve this case:

```php
$my_plugin = WP_PLUGIN_DIR . '/my-plugin';

if ( is_dir( $my_plugin ) ) {
// plugin directory found!
}
```

*Alternative*

The other valid alternative is to compute the path from the URL which is more complex/confusing. I would not use this code:

```php
$plugins_url = plugins_url();
$base_url = get_option( 'siteurl' );
$plugins_dir = str_replace( $base_url, ABSPATH, $plugins_url );
// Now $plugins_dir is same as the WP_PLUGIN_DIR constant.

$my_plugin = $plugins_dir . '/my-plugin';
```

My opinion in this case is: Use the constant `WP_PLUGIN_DIR`
Reply

#18
Yeah as per description of [plugin_dir_path][1] it will give you current plugin file path. But as per what you asking here you can do something like below unfortunately no direct way,

$plugin_dir = ABSPATH . 'wp-content/plugins/plugin-folder/';

*Edit: 18-09-2021*

The best practice is to use with latest version is WP_PLUGIN_DIR as follow:

$plugin_dir = WP_PLUGIN_DIR . '/plugin-folder';


[1]:

[To see links please register here]

Reply

#19
As others have stated, using `plugin_dir_path(__FILE__)` works to get the directory of your plugin if used within the main plugin file (e.g. `plugins/myplugin/myplugin.php`.

But what if you want to know the plugin directory from another file in your plugin? Well what I like to do is define a constant in my main plugin file like this;

// plugins/myplugin/myplugin.php

define('MYPLUGIN_PATH', plugin_dir_path(__FILE__));

Then in other php files within your plugin, you can use `MYPLUGIN_PATH` (a constant) anywhere you like, without knowing the directory structure!
Reply

#20
rather than take the advice of someone who's not using your server (and continue guessing at it...)

why not just load a so-called "Classic" theme, and see what you get?
You're either going to get a relative, or an absolute path. It's not random. figure it out. if you don't like the output, change the function/ args. If you don't know the function names, Install Visual Studio Code, and one of the several WordPress autocomplete / intellisense plugins. It basically writes the code for you.

e.g. ./wp-content/themes/classic/footer.php
```
wp_footer();
echo '<br><strong>plugin_dir_url(dirname(__FILE__))</strong><br> '. plugin_dir_url(dirname(__FILE__));
echo '<br><strong>plugins_url()</strong><br> '. plugins_url();
echo '<br><strong>__FILE__</strong><br> ' .__FILE__ .'<br>';
?>
```

go from there.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through