0

I have a function in my functions file where i output Google Analytics code.

    function mytheme_trackingcode(){
    $mytheme_option = mytheme_get_global_options();
    $tracking_code = $mytheme_option['tracking_code_head'];
    if($tracking_code){
        echo '$tracking_code';
    }
}
add_action('wp_head', 'mytheme_trackingcode');

The problem is that the output comes right after the tag. I'm not sure what the problem is? wp_head is placed last in head-section.

3
  • Can you post the portion of the code that does wp_head()?
    – mor7ifer
    Mar 18, 2012 at 17:15
  • i'm not sure i understand your question. wp_head is a core WordPress action.
    – andeersg
    Mar 18, 2012 at 17:20
  • Yes, a core action executed by wp_head(), which must be called at the correct time by your theme...typically in header.php.
    – mor7ifer
    Mar 18, 2012 at 17:23

1 Answer 1

1

'$tracking_code' ist just a string. It closes <head> automatically. So, no, it doesn’t come after the head, you just failed to use source view. Firebug and similar tools show just the rendered DOM, that’s not good enough.

Fix: echo $tracking_code;.

6
  • I removed the ' and tried again, but i discovered a new problem, WordPress strips the <script> tags.
    – andeersg
    Mar 18, 2012 at 17:35
  • @andeersg No, it doesn’t. I’m 100% sure.
    – fuxia
    Mar 18, 2012 at 18:05
  • I checked again and tried with the "esc_js" function when i save the code. Then the script tags was included, but the code gets " around the code and it is outputed after body tag.
    – andeersg
    Mar 18, 2012 at 18:10
  • Then your save function is wrong, not the output.
    – fuxia
    Mar 18, 2012 at 18:14
  • I'm googling alot now, but can anyone point me in the direction of the function recommended for saving inline-javascript and outputing it?
    – andeersg
    Mar 18, 2012 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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