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'm working on a plugin that adds scripting to the head using

add_action('wp_head',..);

I have to pass a variable to the function to get my desired result. To test this I'm simple "echoing" out the variable to be sure it's passing.

I'm calling the function within a page template. The variable is echoed in the body but not the head:

function add_brand_style($newItem){
    global $newItem;
    echo 'item:'.$newItem;
}
add_action('wp_head','add_brand_style');

the head i get "item:' and in the body I get something like "item:dog".

share|improve this question
1  
You are off ot a bad start by adding scripts using wp_head. You should enqueue scripts properly: codex tut –  vancoder Apr 11 '13 at 18:07
1  
Not to mention wp_head shouldn't take in parameters per the codex. –  dcp3450 Apr 11 '13 at 18:14
1  
Possibly you should also look into localizing scripts: codex –  vancoder Apr 11 '13 at 18:17
1  
Have you read Passing a parameter to filter and action functions? –  toscho Apr 11 '13 at 19:49
1  
I think you should give more details on what you're trying to achieve. Where does $newItem coming from? Different enqueueing techniques are possible with conditional tags. The issue is not "why does the hook not accept external parameters", but "how can this be overcome in my specific use case". toschos' link is priceless, favorited! –  brasofilo Apr 11 '13 at 21:07

1 Answer 1

Well, for me the problem, was including during the post content it would not work, but if i did included in the top of the page template model i created, then it would work if i used something like this: VALID FOR PHP 5.3+

$array=array("keywords"=>"keywords here","description"=>"desc here");
       add_action( 'wp_head', function() use ( $array ) {
    foreach ($array as $key => $value) {
      if($key=="description") {echo "your meta here"}
    }
}, 1 );
share|improve this answer

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.