WordPress.org

Ready to get started?Download WordPress

Forums

Using wp_enqueue_style (6 posts)

  1. emerginggeek
    Member
    Posted 1 year ago #

    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

  2. Prasanna SP
    Member
    Posted 1 year ago #

    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' );
  3. emerginggeek
    Member
    Posted 1 year ago #

    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

  4. Prasanna SP
    Member
    Posted 1 year ago #

    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.

  5. emerginggeek
    Member
    Posted 1 year ago #

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

  6. thoughtwell
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic