Tuesday 29 November 2016

How To Load Custom JavaScript In WordPress


Often times, adding custom JavaScript for analytics tracking is the first type of custom code a WordPress user has to implement. Normally the documentation that you are given with these codes are written as if your site is just a bunch of HTML files. But that’s not how WordPress works. The HTML of your site is dynamically generated. In two recent posts on this site, I discussed JavaScript and the other languages used in WordPress and how to implement custom code on your WordPress site. In this post, I will go into more detail on the best ways to work with JavaScript snippets you need to add to your site.
Ways To Add Custom JavaScript To Your Site
Here is a quick list of options for adding JavaScript to your WordPress site. They all will work, but some are better than others. I’ll go over each in detail in this article:
Load a separate JavaScript file using WordPress’ script loader
Use the wp_footer or wp_head hooks to add the script inline
Use a plugin to add header or footer scripts
Modify your theme to include the script (bad idea)
Use the WordPress post editor (really bad idea not worth discussing)
The first two options involve using WordPress hooks. I wrote a basic
Source: https://managewp.org/articles/13916/how-to-load-custom-javascript-in-wordpress




source https://williechiu40.wordpress.com/2016/11/29/how-to-load-custom-javascript-in-wordpress/

No comments:

Post a Comment