Error in including file . js in wordpress theme

Asked

Viewed 192 times

0

Included a . js file in my theme header.php using the wordpress control panel.

However, the line I entered in the file does not appear on the page when I check the source code.

I’m doing like this:

Wordpress Panel > Appearance > Editor > header.php >

<script type="text/javascript" src="https://protegex.italorodrigo.com.br/contato/municipios.js"></script>

But it’s like I didn’t do anything...

I included the line above between the head tags.

inserir a descrição da imagem aqui

Any hint?

  • Take a look if the site is not with some cache plugin :|

  • @Felipedefarias could not identify any cache plugin. open the themes folder, and include the direct code in the header file, even then when accessing the page, the line is not displayed...

  • You can pass the site?

  • @Felipedefarias https://protegex.italorodrigo.com.br/regioes/ I’ll add in the post a print of how I did it

  • Does your wordpress this using supercache? (is a wordpress plugin)

  • @Guilhermenascimento found a plugin called WP Rocket, it is cache. I did the cleaning and when checking the code of the site, the file appears, but a little different from what I progamei, take a look please: https://protegex.italorodrigo.com.br/regioes

  • @Felipedefarias found a plugin called WP Rocket, it is cache. I did the cleaning and when checking the code of the site, the file appears, but a little different from what I progamei, take a look please: protegex.italorodrigo.com.br/regions

  • I think it is because of the Rocket itself, to optimize everything it seems to merge all . js into one, see in the first line <script src="https://protegex.italorodrigo.com.br/wp-content/plugins/wp-rocket/min/?f=/contato/municipios.js,/contato/municipios.js,/wp-includes/js/jquery/jquery-migrate.min.js,..., this probably happens automatically, even if you don’t want to, the advantage of merging everything into one thing is that it will only download and probably be faster than apart.

  • The problem of not being working is that you have scripts on your site in wrong order, jQuery always has to be the first script, so you have to adjust the order of everything to stop the error from occurring: TypeError: $ is not a function, out that has Javascript in the middle of your page, out of document . js, and how all your scripts run with defer that will fail.

  • About Defer and async read this: https://answall.com/q/304137/3635 - The solution is to remove the loose javascript like this one <script type="text/javascript">var ajaxRevslider; and put in documents with Defer too, and also do not forget that you have problems in the order of your documents.

Show 5 more comments

1 answer

1


I believe that the "problem" is partially in the use of Rocket (https://wp-rocket.me), which is a plugin for caching and optimization of scripts injected into the page

How can you read in their "feautures":

In other words, because of defer will only be processed when the page loads, but in the middle of your site have loose scripts, like this on line 29 and 30:

<script type="text/javascript">var ajaxRevslider;

            jQuery(document).ready(function() {
                // CUSTOM AJAX CONTENT LOADING FUNCTION

Since Rocket can only optimize scripts that are in files .js, then this loose script will not be optimized and due to the use of defer the script will not even be processed, ie it would be better if this script was moved to a file .js


So in conclusion, this is why even writing this:

 <script type="text/javascript" src="https://protegex.italorodrigo.com.br/contato/municipios.js"></script>

You visualize it:

<script type='text/javascript' src='https://protegex.italorodrigo.com.br/wp-content/cache/busting/1/wp-includes-js-jquery-jquery-1.12.4.js'></script>

<script src="//use.typekit.net/e590690a5a7eb8cf5aba4f1e0fc6a9be49cb81a2.js" defer></script>

<script src="https://protegex.italorodrigo.com.br/wp-content/plugins/wp-rocket/min/?f=/contato/municipios.js,/contato/municipios.js,/wp-includes/js/jquery/jquery-migrate.min.js,/wp-includes/js/jquery/ui/core.min.js,/wp-content/plugins/mega-addons-for-visual-composer/js/script.js,/wp-content/plugins/essential-grid/public/assets/js/lightbox.js,/wp-content/plugins/essential-grid/public/assets/js/jquery.themepunch.tools.min.js,/wp-content/plugins/revslider/public/assets/js/jquery.themepunch.revolution.min.js,/wp-content/plugins/woocommerce/assets/js/frontend/add-to-cart.min.js,/wp-content/plugins/js_composer/assets/js/vendors/woocommerce-add-to-cart.js,/wp-content/plugins/testimonial-rotator/js/jquery.cycletwo.js,/wp-content/plugins/testimonial-rotator/js/jquery.cycletwo.addons.js,/wp-content/themes/easyweb/js/jquery.plugins.js" data-minify="1" defer></script>

Something else, probably the municipios.js should be the last, after the jQuery add-ons and plugins, if such municipios.js depend on jQuery, of course.


Read about Defer and async

Browser other questions tagged

You are not signed in. Login or sign up in order to post.