I'm working on a plugin that adds scripting to the head using


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;

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

  • 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? – fuxia 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

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 );

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.