Melissa Chang
Melissa Chang

Reputation: 41

How do i locate wordpress plugin directory?

I am trying to add a function from plugin 1(wp job manager) to plugin 2(woocommerce).

I have decided to do this by including the php file from plugin 1, however I am unable to locate the file directory. I have used:

 include( plugin_dir_path( __FILE__ ) . 'wp-job-manager/includes/class-wp-job-manager-applications.php');

but it returns the following error:

Warning: include(/home/content/p3pnexwpnas05_data02/78/2394078/html/wp-content/themes/listify-child/wp-job-manager/includes/class-wp-job-manager-applications.php): failed to open stream: No such file or directory in /home/content/p3pnexwpnas05_data02/78/2394078/html/wp-content/themes/listify-child/functions.php on line 77

Please advise me as I've been stuck on this issue for really long... Thanks!!!

Upvotes: 4

Views: 6010

Answers (2)

Eric Mahieu
Eric Mahieu

Reputation: 327

Wordpress setups have a constant ABSPATH defined (look at the bottom lines of wp_config.php) which points to the full and absolute path of the Wordpress setup, so in your case echo ABSPATH; would return /home/content/p3pnexwpnas05_data02/78/2394078/html/.

For most installations, appending wp-content/plugins/ to that string would point you to your plugins directory.

However, in a Wordpress configuration one can also customize the wp-content and or plugins directory to their own preference, so building plugins on ABSPATH.'wp-content/plugins/ is not recommended. Unfortunately Wordpress simply doesn't have a get_absolute_pluginspath() function or something available. A trick would be to fetch the plugins-URL, and remove the site-URL from it, so the remaining data is wp-content/plugins/ (or whatever the user has made of it). In code:

$plugins_directory = ABSPATH.str_replace(site_url()."/","",plugins_url())."/";

Which in your case would return: /home/content/p3pnexwpnas05_data02/78/2394078/html/wp-content/plugins/

Upvotes: 2

user488187
user488187

Reputation:

You probably mean:

plugin_dir_path(__FILE__)

That gives you the directory path to the file that statement is in. So what that returns depends on where you run it. If you use this statement

include( plugin_dir_path(__FILE__) . 'wp-job-manager/includes/class-wp-job-manager-applications.php');

in the main plugin file for wp_job_manager (probably wp_job_manager.php), then plugin_dir_path(__FILE__) give the path of the directory that file is in (the plugin directory).

If you use it in some other file, you will need to adjust the rest of the path string accordingly.

Upvotes: 1

Related Questions