• Joe Hall

    (@redbmedia)


    So I have created a short code that displays some custom coded in a post. I want this short code to also insert some javascript in the head of any page that the short code is placed. Here’s what I have so far.

    [Code moderated as per the Forum Rules. Please use the pastebin]

    I understand that I could get the JS to appear on ever page by taking
    add_action('wp_head', 'validate_js'); out of the shortcode function. But, I don’t want the JS on every page, I just want it on the pages/posts that the shortcode is on. Can anyone help me identify what the problem might be with my code?

Viewing 6 replies - 1 through 6 (of 6 total)
  • I need this too. Any answers?

    Im trying to achieve exactly this atm…. does anyone have a solution?

    It’d also be great if you could get the attributes from the shortcode to appear in the string you want to add to wp_head… is that possible?

    Ha! I found a solution to the OP question its here

    It will allow you to register a script if your shortcode is used.

    Turns out what I want to do is share the attribute from the shortcode with another function that I will then add to wp_head. Does anyone know how to do that!?

    Thanks esmi but what am I missing something on those pages…

    I know enough about how to enqueue scripts and use Java…. what I need to know now is how to bring the attributes from my shortcode to the function what will be added to wp_head….?

    Thanks

    Here is a another way to add scripts based upon a shortcode…

    it does add the script to the footer but it turns out shortcodes are processed too late (after the headers have been sent) to add scripts to the head section

    With my problem I’m keeping php functions to a minimum and having the shortcode link the elements to different Java by using different classes…

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Adding JavaScirpt to with a short code??’ is closed to new replies.