0

In functions.php I used the following code to enqueue assets, but for some reason all CSS is ignored (JS isn't ingonred):

function my_theme_enqueue_assets() {
    wp_enqueue_style( 'parent-style', get_parent_theme_file_uri( 'style.css' ) );
    wp_enqueue_script( 'behavior', get_theme_file_uri( 'behavior.js' ), array(), null, true );
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_assets' );

What's bad with the above code?

1
  • In your function, add the following line: error_log( 'Parent Theme Style sheet is: ' . get_parent_theme_file_uri( 'style.css' ) ); Then look for that line in the debug.log located in ./wp-content/. That will tell you if the correct link is being created.
    – Cedon
    Commented Dec 30, 2017 at 18:22

1 Answer 1

1

It seems to me best to separate styles, from scripts and other assets. This worked:

function my_theme_enqueue_styles() {
    wp_enqueue_style( 'parent-style', get_parent_theme_file_uri() . 'style.css' );
}
add_action( 'my_theme_enqueue_styles', 'wp_enqueue_styles' );

function my_theme_enqueue_assets() {
    wp_enqueue_script( 'behavior', get_theme_file_uri( 'behavior.js' ), array(), null, true );
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_assets' );
9
  • 1
    While the separation of concerns is fine, use of get_template_directory_uri() for the parent style is outdated because of problems that they caused with same files names. So, for example, you cannot have style.css in the child theme as well. Every file has to be unique between both parent and child theme. That's why the WP Team created these new functions. There is a good explanation here
    – Cedon
    Commented Dec 30, 2017 at 18:45
  • The two separate functions in the question caused the site to lack styling. You are more than welcome to write an answer that will show where I was wrong. Commented Dec 30, 2017 at 19:10
  • Did you follow my suggestion in the comment I made to your question? Because that will tell you if the proper URL is being used.
    – Cedon
    Commented Dec 30, 2017 at 19:18
  • No, I don't know what URL you mean to. I load the parent theme CSS, the child theme CSS and the pagebuilder (Elementor) CSS. I don't know what URLs take part in this, I don't to load a specific style, rather, all stylesheets available. Commented Dec 30, 2017 at 19:49
  • I've edited the answer @Cedon Commented Dec 30, 2017 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.