1//current path: /home/user/var/www/wordpress/wp-content/plugins/my-plugin/
2$dir = plugin_dir_path( __DIR__ );
3//$dir is set to /home/user/var/www/wordpress/wp-content/plugins/
4
1if ( is_admin() ) {
2 include_once( plugin_dir_path( __FILE__ ) . 'includes/admin-functions.php' );
3} else {
4 include_once( plugin_dir_path( __FILE__ ) . 'includes/front-end-functions.php' );
5}
6
1function enqueue_scripts() {
2 wp_enqueue_script( 'custom-js', plugin_dir_url( __FILE__ ) . 'js/custom.js', array( 'jquery' ), '', true );
3 wp_enqueue_style( 'style-css', plugin_dir_url( __FILE__ ) . 'css/style.css' );
4}
5add_action( 'wp_enqueue_scripts', 'enqueue_scripts');
6
7function admin_enqueue_scripts() {
8 wp_enqueue_script( 'custom-js', plugin_dir_url( __FILE__ ) . 'js/custom.js', array( 'jquery' ), '', true );
9 wp_enqueue_style( 'style-css', plugin_dir_url( __FILE__ ) . 'css/style.css' );
10}
11add_action( 'admin_enqueue_scripts', 'admin_enqueue_scripts');
12
1define( 'MY_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
2include( MY_PLUGIN_PATH . 'includes/admin-page.php');
3include( MY_PLUGIN_PATH . 'includes/classes.php');
4// etc.
5
1foreach ( glob( plugin_dir_path( __FILE__ ) . "subfolder/*.php" ) as $file ) {
2 include_once $file;
3}
4
1$dir = plugin_dir_path( __FILE__ );
2// Example: /home/user/var/www/wordpress/wp-content/plugins/my-plugin/
3
1/**
2 * Include CSS file for MyPlugin.
3 */
4function myplugin_scripts() {
5 wp_register_style( 'foo-styles', plugin_dir_url( __FILE__ ) . 'assets/foo-styles.css' );
6 wp_enqueue_style( 'foo-styles' );
7}
8add_action( 'wp_enqueue_scripts', 'myplugin_scripts' );
9
10Answer
11http://example.com/wp-content/plugins/my-plugin/assets/foo-styles.css
12
1function plugin_dir_path( $file ) {
2 return trailingslashit( dirname( $file ) );
3}
4