Search code examples
phpcsswordpresscustom-wordpress-pageswordpress-gutenberg

Creating a wordpress plugin - plugin not finding CSS files


I'm creating a wordpress plugin and getting 404 not found on css files. Heres the code (it's in the main php file for the plugin)

function callback_for_setting_up_scripts() {
    wp_enqueue_style('bootstrap', 'https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css', ver:null);
    wp_enqueue_style('main-sincoe-style', __DIR__.'/css/main.css', ver:null);
    echo  readfile(__DIR__.'/css/main.css');
}

add_action('wp_enqueue_scripts', 'callback_for_setting_up_scripts');

So two things:

  • 1st echo works as it should - it reads out the whole css file - so it shouldn't be a path problem as far as I know.
  • 2nd Bootstrap works as it should so there should not be a problem in the function

So i'm stuck a bit here - thanks for any help (I'm on Mac if that changes anything - just moved so maybe those are some write/read permissions that i have not put up yet)

Thanks in advance


Solution

  • Thanks @Moshe Gross

    plugin_dir_url() worked - the working code:

    function callback_for_setting_up_scripts() {
        wp_enqueue_style('bootstrap', 'https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css', ver:null);
        wp_enqueue_style('main-sincoe-style',plugin_dir_url(__FILE__).'/css/main.css', ver:null);
    }
    
    add_action('wp_enqueue_scripts', 'callback_for_setting_up_scripts');