How Am I supposed to add dynamic JS depending on jQuery? (5 posts)

  1. SocialBlogsite
    Posted 4 years ago #

    My script needs to run AFTER jQuery is enqueued, but print_scripts runs BEFORE jQuery is loaded.

    I can't put it in a file, since it's calculated by other parts of the theme/plugins.

    …or is there another action that runs after "print_scripts" I can hook it to?

  2. stvwlf
    Posted 4 years ago #

    Use this function to enqueue the script
    and declare that its depedency is on jQuery - that insures WordPress loads your script after jQuery's been loaded.

    Here's an article

    You don't need register_Scripts.

    As of WP 3.3 WP recommends using the wp_enqueue_scripts hook to load both scripts and stylesheets, no longer recommend using the print_scripts hook.

  3. SocialBlogsite
    Posted 4 years ago #

    i probably wasn't clear:
    I don't want to save the script in a separate file.

    I need it rendered with the page, because it's DYNAMIC.

    Please tell my why it wasn't clear. It helps with my english.


  4. stvwlf
    Posted 4 years ago #

    Sorry, I missed that part of what you said.

    use the wp_head hook - jQuery will be loaded before that one

    or if you are adding it into the header.php file, enter it after the call to wp_head().

  5. SocialBlogsite
    Posted 4 years ago #

    Yes, That's what I did for now. I've put it in the header.php file, but eventually I'll make it a plugin, so I need it to work from a function outside the theme.

    I will try the wp_head. I hope I don't bother whatever wp_head needs to return.

Topic Closed

This topic has been closed to new replies.

About this Topic