1

I've been reading Wordpress Theme Developer Handbook to learn how things work and try to practice according to what's written in there.

I've created my index.php, style.css, function.php and header and footer files. According to the Including CSS & JavaScript Part . it says that other than directly use the link tag, i need to create a functions.php and in to that file i need to add wp_enqueue_style( 'style', get_stylesheet_uri() ); function with these parameters.

I am adding this line of code to my functions.php but I can't see the file when I look at the source of the page.

I also tried to add this function to my header.php instead of functions.php , it doesn't seem to work.

1 Answer 1

0

Your call to wp_enqueue_style() looks good and functions.php is the right place, but your code should be wrapped inside a function attached to the wp_enqueue_scripts hook. See the Combining Enqueue Functions section of the page you referenced for a full example. Generally speaking, any code you run in WordPress should be run on some hook or filter. Otherwise the code will be run as soon as the code is read which is usually not the correct time.

function mytheme_add_theme_scripts() {
  wp_enqueue_style( 'style', get_stylesheet_uri() );
}
add_action( 'wp_enqueue_scripts', 'mytheme_add_theme_scripts' );
2
  • Thank you so much. But in the handbook they didn't mention this specifically because it says "It is best to combine", not a must. I thought that because I am not gonna use any script right now, no need to combine anything and did not write like that. I wonder where did the first theme developers learn these rules from. Does not this mean handbook is lack of some of the needed information? I wanted to not looking any other tutorials, to get an experience of learning something using only documentations but i got so confused now. Commented Jul 20, 2019 at 22:56
  • You got it, NoobDev. I don't know why they don't start off with an example like I showed in my answer, which doesn't use any script dependencies (e.g. combining example), but does properly enqueue the stylesheet at the right time. The handbook is generally pretty good though. Another good way of learning best practices is to look at how the bundled themes (named by the year they are packaged, e.g. Twenty Nineteen) do things. A good starter theme is _s (underscores) and you've already found a good place to ask questions here at WPSE. Commented Jul 21, 2019 at 0:17

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.