• Hey there,

    I have been working on a custom theme and am trying to make a function that adds stylesheets to my header via a wp_hook for custom pages.

    What I want to be able to do is:

    <?php
    /* Template Name: Home Page
    */
    
    add_stylesheet('home.css');
    ?>

    But, the closest I can get is:

    if(!function_exists('add_stylesheet')):
    function add_stylesheet($path) {
    	$template_url = get_bloginfo('template_url');
    	echo '<link rel="stylesheet" type="text/css" href="'.$template_url.'/'.$path.'" />';
    }
    add_action('stylesheet_hook',function () {
    	return add_stylesheet('home.css');
    });
    endif;

    The reason I can’t get the first result is because I can’t pass a variable into the add_action scope. What “theoretically” the code would look like (if I got my way) is this:

    function new_stylesheet($path) {
            $template_url = get_bloginfo('template_url');
    	echo '<link rel="stylesheet" type="text/css" href="'.$template_url.'/'.$path.'" />';
    }

    Here is where my issue comes in. I can’t pass $path into the add_action scope. Is there a solution? I may not know enough PHP to see it?

    function add_stylesheet($path) {
            add_action('stylesheet_hook',function () {
              return new_stylesheet($path);
            });
    }

    I would be grateful for any help.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    I’m probably missing it completely, and if so I apologize right now, but try this to load home.css via your template file.

    wp_enqueue_style( 'home' , get_stylesheet_directory_uri() . '/home.css' , '' , '' );

    On my test template in a child theme I added that right above the get_header() line. I think it will work on a parent theme too but check it.

    Edit: use get_stylesheet_directory_uri() instead, I’ve updated the line above.

    Thread Starter nomadgraphix

    (@nomadgraphix)

    Hey Jan,

    Thanks for the response.

    Your solution works flawlessly!

    but…

    I am using a custom hook to add my stylesheets the the header because I am attempting to improve my site performance by having this order in my header:

    1. Stylesheets
    2. Other <link> tags.
    3. Scripts

    My custom hook code in my function.php file is:

    if(!function_exists('stylesheet_hook')):
    function stylesheet_hook() {
    	do_action('stylesheet_hook');
    }
    endif;

    Then, my header has the hook right after the main stylesheets:

    <link rel="shortcut icon" href="<?php bloginfo('template_url') ?>/images/logo.ico" />
    <link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo( 'stylesheet_url' ); ?>" />
    <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/nivo-slider/nivo-slider.css" type="text/css" media="screen" />
    <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/nivo-slider/themes/default/default.css" type="text/css" media="screen" />
    <?php
    stylesheet_hook(); ?>

    Which hook in the header does wp_enqueue_style() use? The “enqueue_script()”?

    Moderator keesiemeijer

    (@keesiemeijer)

    Thread Starter nomadgraphix

    (@nomadgraphix)

    Keesiemeijer,

    This is fine, but I want to add the stylesheet before all scripts…and I want a simple statement at the beginning of each template:

    add_stylesheet(‘home.css’);

    That is why I am using a custom hook.

    Moderator keesiemeijer

    (@keesiemeijer)

    Your stylesheets are added before any other stylesheets or scripts in the output of wp_head() when you enqueue it. Conditional tags are also available so you could do something like this [untested]:

    function theme_styles()  { 
    
      wp_register_style( 'home',  get_stylesheet_directory_uri() . '/home.css', array(), false, 'all' );
      wp_register_style( 'category',  get_stylesheet_directory_uri() . '/category.css', array(), false, 'all' );
    
      if(is_home()) {
      	wp_enqueue_style( 'home' );
      }
    
      if(is_category()){
    	wp_enqueue_style( 'category' );
      }
    }
    add_action('wp_enqueue_scripts', 'theme_styles');

    Or just hardcode it in your header:

    <?php if(is_home()) : ?>
    <link rel="stylesheet" type="text/css" media="all" href="<?php echo get_stylesheet_directory_uri().'/home.css'; ?>" />
    <?php endif; ?>
    
    <?php if(is_category()) : ?>
    <link rel="stylesheet" type="text/css" media="all" href="<?php echo get_stylesheet_directory_uri().'/category.css'; ?>" />
    <?php endif; ?>
    
    <?php
    // all other stylesheets and scripts
    wp_head();
    ?>

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Passing variables into a wordpress hook.’ is closed to new replies.