I want to include a slider in the default Wordpress theme on the home page. I did manage to get the page to show using <?php if ( is_home() ) { include ('slider.php'); } ?> but it doesn't look like it's loading the jquery script it needs or the style sheet. I know there's a conflict with scripts and Wordpress, I just don't know how to get it to work.

Sample page is at http://axiomwest.com/

How do you feed in a page with it's own style sheet and scripts?


You need to have the script in a separate file (normally it would be filename.js; I suppose filename.php would work?).

Then, you need to register and enqueue that script file, using wp_register_script() and wp_enqueue_script()


function mytheme_register_custom_scripts() {
    if ( ! is_admin() ) {
        $scriptsrc = get_stylesheet_directory_uri() . '/scripts/filename.js';
        wp_register_script( 'mytheme_slider', $scriptsrc );
add_action( 'after_setup_theme', 'mytheme_register_custom_scripts' );

function mytheme_enqueue_custom_scripts() {
    if ( is_home() ) {
        wp_enqueue_script( 'mytheme_slider' );
add_action( 'wp_enqueue_scripts', 'mytheme_enqueue_custom_scripts' );

Note that registering the script should happen at after_setup_theme, but is_home() will not be available at this point I don't think, which is why you need to separate the enqueueing function so that it hooks into wp_head, by which time is_home() is available.

  • wp_enqueue_scripts is more appropriate hook than wp_head for such, made for that and less likely to get timing wrong.
    – Rarst
    Apr 11 '11 at 19:02
  • For which? The scripts could be registered at wp_enqueue_scripts, but if he only wants to enqueue the script when is_home() is true, he'll have to do that at wp_head, right? Apr 11 '11 at 19:06
  • @Chip Bennett for either. wp_enqueue_scripts is kinda... sub-hook (did I just invent the term?..) of wp_head.
    – Rarst
    Apr 11 '11 at 19:56
  • Rarst is right. wp_enqueue_scripts runs as part of a function hooked onto wp_head at priority 1, so wp_head at any priority greater than 0 is too late. Apr 11 '11 at 19:57
  • Okay, updated to change the enqueue to hook into wp_enqueue_scripts rather than wp_head. :) Apr 11 '11 at 20:29

UPDATE! What I did was just include the slider I was trying to use. I linked the jquery scripts that page needs via the <?php bloginfo('template_directory'); ?> method. I did notice that it takes a couple of seconds to load that portion of the page, but it works. I still think this can be improved, but with time constraints it will have to do. Thanks all for your comments and eagerness to help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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