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 written a small class that will handle enqueues in my theme :

<?php

class Header {

    public function init_hooks()
    {
        add_action('wp_print_scripts', array(__CLASS__,'include_all_files'));
    }

    public function include_css_files()
    {
        wp_register_script('style.css', get_bloginfo('stylesheet_url'));
        wp_enqueue_script('style.css');
    }

    public function include_js_files()
    {
        wp_register_script('jquery-min', "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js");
        wp_enqueue_script('jquery-min');        
    }

    public function include_all_files()
    {
        $this->include_css_files();
        $this->include_js_files();
    }

}

?>

I call it right before closing the head tag, like :

$header = new Header();
$header->init_hooks(); 

But it does not work. There is no error, but no script is added. Any ideas ?

share|improve this question
    
You're code is running after scripts have been output(you said just before the closing <head> tag, that's too late for the enqueues. The code needs to run before wp_head() does.. –  Mark Duncan Jun 30 '11 at 11:06

1 Answer 1

up vote 2 down vote accepted

You mean you call your class right before closing HTML tag? It is too late to enqueue scripts then.

Don't hook into wp_print_scripts to enqueue your scripts and styles. I just learned this today from Rarst.

So change your init_hooks function to something like following:

public function init_hooks()
{
    add_action('wp_enqueue_scripts', array(__CLASS__,'include_all_files'));
}

Also you are using wp_register_script and wp_enqueue_script functions to enqueue your styles, Wordpress has separate functions to enqueue your styles wp_register_style and wp_enqueue_style. Your include_css_files should look like:

public function include_css_files()
{
    // NOTE: you don't really have to use the filename as style slug.
    wp_register_style('my_style', get_bloginfo('stylesheet_url'));
    wp_enqueue_style('my_style');
}

Now about instantiating your class and calling init_hooks. You can either place it directly inside your functions.php, or you can use the init action to instantiate your class.

share|improve this answer
    
very helpful thank you :) –  SpyrosP Jun 29 '11 at 5:15
    
you are welcome –  Hameedullah Khan Jun 29 '11 at 5:17
    
just a small side question. I can't get the style to print without having something like "wp_print_scripts('style.css');" explicitly. Is this normal ? It does not like normal to me :/ –  SpyrosP Jun 29 '11 at 5:35
    
Oops.. sorry I missed you are using wp_register_script and wp_enqueue_script to register and enqueue your styles. There are separate functions for styles, wp_register_style and wp_enqueue_style. See codex.wordpress.org/Function_Reference/wp_enqueue_style and codex.wordpress.org/Function_Reference/wp_register_style –  Hameedullah Khan Jun 29 '11 at 6:45
    
Updated my answer. –  Hameedullah Khan Jun 29 '11 at 6:51

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.