Skip to content

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

Open
@remcotolsma

Description

@remcotolsma

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions