Passing variables into a wordpress hook.
-
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)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘Passing variables into a wordpress hook.’ is closed to new replies.