• Hi all,

    I am VERY new to WordPress, so this might be a pretty basic question.

    I have been playing with theme development for a few weeks and have decided to start from scratch, not even using a reset theme, just to figure out how things go together.

    I have heard that the best practice is to use the wp_enqueue_style to call your css. I can, and have copied and pasted the href=”<?php bloginfo( ‘stylesheet_url’ ); ?> from the twentyeleven theme, and it works, but I would like to do it the right way.

    I might be thick, but I am not sure how to use it. If someone could explain like I’m five, I would appreciate it. My specific question is do you use the wp_register_style in the themes function.php? Or is it already in wp .phps somewhere? Do I add style.css to the parameters, nothing, or the absolute file path?

    Any help would be appreciated, and you cannot get too basic with me.

    Thanks all

Viewing 5 replies - 1 through 5 (of 5 total)
  • Register the stylesheet first

    function register_my_theme_styles(){
            if ( ! is_admin() ){
                    wp_register_style( 'my-theme-stylesheet', get_stylesheet_uri(), array(), false, 'screen' );
            }
    }
    add_action( 'init', 'register_my_theme_styles' );

    Then enqueue it

    function enqueue_my_theme_styles(){
            if ( ! is_admin() ){
                    wp_enqueue_style( 'my-theme-stylesheet' );
            }
    }
    add_action( 'wp_enqueue_scripts', 'enqueue_my_theme_styles' );

    get_stylesheet_uri gets the style.css URL of your theme. If you want to register other style sheets (say ie.css), use get_template_directory_uri(). Example, to register ie.css style sheet.

    wp_register_style( 'my-theme-ie-stylesheet', get_template_directory_uri() . '/ie.css', array(), false, 'screen' );

    then enqueue it (in the enqueue_my_theme_styles function)

    wp_enqueue_style( 'my-theme-ie-stylesheet' );
    Thread Starter emerginggeek

    (@emerginggeek)

    Do I register the stylesheet in the functions.php? I assume I enqueue it in the head.php?

    Thanks for your help. I struggle sometimes!

    Dave

    Yup, this goes in functions.php. But, no need to add anything to head.php. When you enqueue it, WordPress automatically adds reference to the style sheet in head.

    EDIT:
    Both registering and enqueueing style sheet is done in functions.php itself.

    Thread Starter emerginggeek

    (@emerginggeek)

    Great! Thanks for your help. I am sure that I will be back as I keep working at this!

    Thanks for this, Prasanna. Works beautifully so far in 3.5 w/ the Bones Framework I’m using. I needed to call some styles for a webfont service, and this works great. I’ll have to look a little closer sometime to learn what the ! is_admin function does.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Using wp_enqueue_style’ is closed to new replies.