Skip to content

WordPress Plugin Review Team → Determine files and directories locations correctly #1256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
remcotolsma opened this issue Mar 27, 2025 · 0 comments

Comments

@remcotolsma
Copy link
Contributor

remcotolsma commented Mar 27, 2025

We received the following feedback from the WordPress Plugin Review Team about a plugin that ships Action Scheduler library.

## Determine files and directories locations correctly

WordPress provides several functions for easily determining where a given file or directory lives.

We detected that the way your plugin references some files, directories and/or URLs may not work with all WordPress setups. This happens because there are hardcoded references or you are using the WordPress internal constants.

Let's improve it, please check out the following documentation:

https://developer.wordpress.org/plugins/plugin-basics/determining-plugin-and-content-directories/

It contains all the functions available to determine locations correctly.

Most common cases in plugins can be solved using the following functions:
For where your plugin is located: plugin_dir_path() , plugin_dir_url() , plugins_url()
For the uploads directory: wp_upload_dir() (Note: If you need to write files, please do so in a folder in the uploads directory, not in your plugin directories).

Example(s) from your plugin:

packages/woocommerce/action-scheduler/classes/ActionScheduler_SystemInformation.php:28 $plugin_file = trailingslashit( WP_PLUGIN_DIR ) . $plugin_file;
packages/woocommerce/action-scheduler/classes/WP_CLI/System_Command.php:156 $path = str_replace( ABSPATH, '', $path );
packages/woocommerce/action-scheduler/classes/WP_CLI/System_Command.php:178 $path = str_replace( ABSPATH, '', $path );

ℹ️ In order to determine your plugin location, you would need to use the FILE variable for this to work properly.
Note that this variable depends on the location of the file making the call. As this can create confusion, a common practice is to save its value in a define() in the main file of your plugin so that you don't have to worry about this.

Example: Your main plugin file.

define( 'MYPREFIX_PLUGIN_FILE', __FILE__ );
define( 'MYPREFIX_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'MYPREFIX_PLUGIN_URL', plugin_dir_url( __FILE__ ) );

Example: Any file of your plugin.

require_once MYPREFIX_PLUGIN_DIR . 'admin/class-init.php';

function myprefix_scripts() {
 wp_enqueue_script( 'myprefix-script', MYPREFIX_PLUGIN_URL . 'js/script.js', array(), MYPREFIX_VERSION );
 // Or alternatively
 wp_enqueue_script( 'myprefix-script', plugins_url( 'js/script.js', MYPREFIX_PLUGIN_FILE ), array(), MYPREFIX_VERSION );
}
add_action( 'wp_enqueue_scripts', 'myprefix_scripts' );

Example(s) from your plugin:

packages/woocommerce/action-scheduler/classes/ActionScheduler_SystemInformation.php:28 $plugin_file = trailingslashit( WP_PLUGIN_DIR ) . $plugin_file;

Previously reported similar issues:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant