Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have the following code in my functions.php file. It works, but I am wondering if this is best practice for including styles in wordpress. Also do I need to register the style with wp_reister_style, since it works without and why do I need to do so?

<?php
function mrskitson_load_styles(){
if (!is_admin()){
wp_register_style('main', get_template_directory_uri() . '/style.css');
wp_enqueue_style('main', get_template_directory_uri() . '/style.css');
}//if not admin ends
}//function mrskitson_load_styles ends
add_action('get_header', 'mrskitson_load_styles');
?>
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Two problems:

  1. You only need to provide the script path once, either in the wp_register_style() call, or in the wp_enqueue_style() call, but not both. (In fact, in this context, it would be fine to omit the wp_register_style() call entirely, and just use the wp_enqueue_style() call.)
  2. You're hooking into the wrong action. Try hooking into wp_enqueue_scripots instead of into get_header. (Note: this will require the Theme to include a call to <?php wp_head(); ?> in the document head - but it should be doing so, anyway.)

Other considerations:

  • For custom styles/scripts, I would recommend prefixing the style slug; e.g. change main to srskitson-main.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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