admin管理员组

文章数量:1122846

I hvae a plugin which includes php file from another plugin.

So I would do this:

plugin 'foo' includes file from plugin 'blah':

wp-content/plugins/foo

include(__DIR__."/../blah/some-file.php");  

This works but I am sure if I like this ../ , I wanted to reference plugin directory like:

include( WP_PLUGIN_DIR.'/blah/some-file.php' );

This works as well, but it say here:

WordPress makes use of the following constants when determining the path to the content and plugin directories. These should not be used directly by plugins or themes, but are listed here for completeness.

I cannot use plugin_dir_url() without an argument, like:

include( plugin_dir_url().'/blah/some-file.php' );

I can only do this:

include( plugin_dir_url(__FILE__).'/../blah/some-file.php' );

which is the same as first include above with DIR

I hvae a plugin which includes php file from another plugin.

So I would do this:

plugin 'foo' includes file from plugin 'blah':

wp-content/plugins/foo

include(__DIR__."/../blah/some-file.php");  

This works but I am sure if I like this ../ , I wanted to reference plugin directory like:

include( WP_PLUGIN_DIR.'/blah/some-file.php' );

This works as well, but it say here:

WordPress makes use of the following constants when determining the path to the content and plugin directories. These should not be used directly by plugins or themes, but are listed here for completeness.

https://codex.wordpress.org/Determining_Plugin_and_Content_Directories

I cannot use plugin_dir_url() without an argument, like:

include( plugin_dir_url().'/blah/some-file.php' );

I can only do this:

include( plugin_dir_url(__FILE__).'/../blah/some-file.php' );

which is the same as first include above with DIR

Share Improve this question asked Aug 21, 2024 at 7:49 ToniqToniq 4476 silver badges15 bronze badges 2
  • I think that __DIR__ can be used in this case. but do you really need that ? you will have an error if you deactivate the plugin with the included file. I recommend to use communication between plugins like that : wordpress.stackexchange.com/questions/426353/… – mmm Commented Aug 21, 2024 at 11:54
  • I use constant in main plugin (defined in other plugin) to check if other plugin is activated. Strange that plugin_dir_url cannot return plugins directory on its own. – Toniq Commented Aug 21, 2024 at 14:39
Add a comment  | 

1 Answer 1

Reset to default 0

It doesn't make a lot of sense, to me, to say "these should not be used directly by plugins". I tend to think you should use an available function over the constant - if an available function exists - which, in your case, does not. I would go ahead and use the WP_PLUGIN_DIR constant.

本文标签: plugin developmentUsing WPPLUGINDIR for include file