Plugin throws up 404 on front-end when when enqueuing style with filetime

You’re getting the 404 error because you didn’t provide the correct URL address of your CSS file.

And that’s because of the first plugin_dir_path() below which outputs a filesystem directory path (e.g. /home/user/var/www/wordpress/wp-content/plugins/my-plugin/):

wp_enqueue_style( 'myplugin-style', plugin_dir_path( __FILE__ ) . '/css/style.css', array(), filemtime( plugin_dir_path( __FILE__ ) . '/css/style.css' ) );

So you should instead use plugin_dir_url() for getting the URL directory path for your plugin (e.g. http://example.com/wp-content/plugins/my-plugin/):

wp_enqueue_style( 'myplugin-style', plugin_dir_url( __FILE__ ) . 'css/style.css', array(), filemtime( plugin_dir_path( __FILE__ ) . '/css/style.css' ) );

And note that both the functions include a trailing slash, so in the above code, I intentionally used css/style.css and not /css/style.css. Otherwise, the URL would contain //css/style.css (note the two slashes).

Leave a Comment