Ready to get started?Download WordPress


Adding JavaScirpt to with a short code?? (7 posts)

  1. Joe Hall
    Posted 4 years ago #

    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?

  2. lerizzle
    Posted 3 years ago #

    I need this too. Any answers?

  3. mtwelve
    Posted 3 years ago #

    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?

  4. esmi
    Theme Diva & Forum Moderator
    Posted 3 years ago #

  5. mtwelve
    Posted 3 years ago #

    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!?

  6. mtwelve
    Posted 3 years ago #

    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....?


  7. mtwelve
    Posted 3 years ago #

    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...

Topic Closed

This topic has been closed to new replies.

About this Topic