php – WordPress “wp_register_style was called incorrectly”?-ThrowExceptions

Exception or error:

I have a question. When i want to put this code in my first template for WordPress, not works. I have a library for bootstrap, and one more for my style. Why? Below is my code. Thank you 😀

function load_stylesheets() // funtion name
    {
     wp_register_style('style', get_template_directory_uri() . 'style.css', array(), false, 'all');//style.css
     wp_enqueue_style('style');

     wp_register_style('bootstrap', get_template_directory_uri() . '/css/bootstrap.min.css', array(), false, 'all');// this is for bootstrap.
     wp_enqueue_style('bootstrap');

}

// Add Action

  add_action('wp_enqueue_scripts', 'load_stylesheets');

//my front-page.php

<div class="container">


    <div class="row">

            <div class="col">
                Left    
            </div>

            <div class="col">
                Right
            </div>

    </div>

//style.css

body{

background-color: red;
 }
How to solve:

Maybe I’m doing this wrong but I cant comment yet however I was going to say to try and take out the optional params from the wp_register_style and just make it:

wp_register_style('my-style', get_template_directory_uri() . 'style.css');
wp_enqueue_style('my-style');

You can see the optional Parameters here: https://developer.wordpress.org/reference/functions/wp_register_style/

don’t forget to change the name of the enqueue to the name of your style…if you changed the name(s) of your styles

Also, just making sure because Bootstrap styesheet is in the css directory, is the style.css in the same directory or is style.css in your main template directory? If its in the main template directory then its fine but if its in the css directory you need to make sure you add /css/style.css to your wp_register_style

Answer:

Try with below?:

// style.css
wp_enqueue_style( 'custom-style', get_theme_file_uri( '/css/style.css' ), array( 'all-style' ), true );

// bootstrap.min.css
wp_enqueue_style( 'bootstrap', get_theme_file_uri( '/css/bootstrap.min.css' ), array( 'all-style' ), true );

Make sure the files are available in the respective folder.

Leave a Reply

Your email address will not be published. Required fields are marked *