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 ?
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:
Also you are using
wp_register_script
andwp_enqueue_script
functions to enqueue your styles, WordPress has separate functions to enqueue your styleswp_register_style
andwp_enqueue_style
. Yourinclude_css_files
should look like: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.