Reputation: 3831
I am attempting to enqueue a stylesheet for my admin plugins page.The file I am working in has this current path,
\plugins\my-plugin-name\includes\fileiamworkingon.php
All I am looking to do is enqueue a stylesheet that sits one directory up from the directory I am currently in. The path to this stylesheet is like so,
\plugins\my-plugin-name\css\styles.css
I currently have it working by calling an add action hook inside of my classes constructor function like this:
add_action('admin_enqueue_scripts', array($this, 'pluginStyles'));
As you can see, it then calls a pluginStyles function where I perform the following code,
public function pluginStyles() {
wp_register_style('my_plugin_stylesheet', plugin_dir_url(__FILE__) . '../css/styles.css' );
wp_enqueue_style('my_plugin_stylesheet');
}
Currently this is the only way I can get it to work because of adding that little subtle '../' to the file path. Otherwise, it tries to access the css folder from inside the 'includes' directory, which obviously no css folder exists in there.
This doesn't seem like the best solution, and maybe there is a more "Wordpress Way" of solving this.
Upvotes: 1
Views: 1114
Reputation: 532
Here is what I've done in the past - define a constant in the main \plugins\my-plugin-name\my-plugin-name.php
file:
/** Define plugin path constant */
if (!defined('PLUGIN_PATH')) {
define('PLUGIN_PATH', plugin_dir_url(__FILE__));
}
And enqueue in your \includes\fileiamworkingon.php
file:
/** Enqueue from any other file in the plugin directory */
wp_register_style('my_plugin_stylesheet', PLUGIN_PATH . 'css/styles.css' );
wp_enqueue_style('my_plugin_stylesheet');
Upvotes: 3
Reputation: 219
Try the following in 'fileiamworkingon.php':
wp_enqueue_style('styles', plugin_dir_path() . '../css/styles.css', array(), null);
The '../' will move you back a directory.
Upvotes: 0
Reputation: 67776
You can get the plugin directory path using plugin_dir_url( __FILE__ );
So you should be able to register your stylesheet this way:
function my_plugin_scripts() {
wp_register_style( 'my_plugin_stylesheet', plugin_dir_url( __FILE__ ) . 'my-plugin-name/css/styles.css' );
wp_enqueue_style( 'my_plugin_stylesheet' );
}
add_action( 'wp_enqueue_scripts', 'my_plugin_scripts' );
Upvotes: 2