• KeineKaefer

    (@keinekaefer)


    Hi Guys,

    do I always have to use the wp_enqueue_script function for my own javascript based on the build-in JQuery in own wordpress templates?

    There are three different types of own written scripts that will be used in different own written templates:

    1.) Generating and setting a variable used later in javascript.
    2.) A tiny script that submits an HTML form if the user presses “Return/Enter” in an input field.
    3.) A longer script that handles some user inputs and calculates some values and gives it back to the page.

    These scripts do not depend on other wordpress scripts. Should I still use the wp_enqueue_script function? Is that necessary for caching purposes?

    Since it depends on the user input which script will be needed in the page, it looks I would add a lot of complexity to project by conditional enqueue the script in the functions.php and I cannot see any advantage by doing so.

    Thank you very much in advance.

    KeineKaefer

Viewing 7 replies - 1 through 7 (of 7 total)
  • @mercime

    (@mercime)

    Thread Starter KeineKaefer

    (@keinekaefer)

    Hey @mercime,

    thank you very much for your response. I’m not really happy with your answer because you basically just referring to what is written in the manual.

    However, I have not the slightest clue why it should be necessary to a register a script that creates a Javascript variable like ‘Amount = 5;’ or code like ‘if ( e.which === 13 ) { $(e.target).closest(‘form’).submit(); }’.

    As I said I conditionally add these scripts. In order to do so I would need a lot of repetitive code with database queries in functions.php.

    Do you really propose that I should boost the complexity of the project and not even know why I’m doing this?

    Thank you in advance and best regards!

    Moderator bcworkz

    (@bcworkz)

    Using wp_enqueue_script() will properly establish the dependency on jquery and minimizes the likelihood of conflicts with other scripts. You may very well get way with not enqueuing your script, but if conflicts occur, tracking them down can be very difficult. I don’t think it’s worth the risk.

    If you need code to conditionally enqueue scripts, you need code to conditionally insert script or link tags, so I don’t see much difference there. TBH, if your scripts are that small, there is little penalty to always enqueuing the script whether it is used or not.

    Thread Starter KeineKaefer

    (@keinekaefer)

    bcworkz,

    thank you very much for your answer. Now I understand why to enqueue the scripts.

    I have one follow up question:
    Is it really necessary to use wp_localize_script for creating Javascript variables? What are the drawbacks when creating these variables with a PHP-script?

    Thank you in advance.

    Moderator bcworkz

    (@bcworkz)

    It is not strictly necessary,but I like it. It provides an easy to follow path where values in javascript come from in PHP. Sure, a similar relationship exists when you echo out a javascript var assignment in PHP. For some reason, that construct never gets my attention. When I see a data array in wp_localize_script(), something goes off in my head “Hey, this is important stuff, pay attention to data assigned here!” It makes long forgotten code easier for me to get reacquainted with.

    I also don’t like script blocks in my HTML, I prefer linked javascript files, it appeals to my desire to stay organized. The localized vars are an easy way to assign PHP values to vars used in external javascript files. In summary, I guess the main advantage for me is it allows me to write cleaner, more organized, more readable code. I’d hate to have to do without.

    A small script like the examples above it probably best to output direcly into the HTML by either hooking into wp_header och wp_footer depending on where you want the script. At least it’s best performance wise as that small script still would add another HTTP request which slows the site down even it’s a small script.

    Another good reason to use wp_enqueue_script() is that you can control the output in your theme much easier.
    Many times you don’t want to load all the unnecessary scripts and styles that all the different plugins add. If the plugins use wp_enqueue_script() you can easily filter and remove scripts and styles as you want.
    Or if you want to make changes to the script that is loaded from a plugin you can just overwrite the script to use your own version instead of editing the in the plugin witch is very bad since the changes are lost when you update the plugin.
    Both of these examples are impossible to do in a good and reliable way without wp_enqueue_*.

    Thread Starter KeineKaefer

    (@keinekaefer)

    Hey bcworkz and pekz0r,

    thank you very much for your answers. You are very kind.

    Now I really understand the concept better.

    Best regards. 🙂

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Always use wp_enqueue_script?’ is closed to new replies.