How to Easily Add JavaScript in WordPress Pages or Posts (3 Methods)

How to Easily Add JavaScript in WordPress Pages or Posts (3 Methods)
Do you want to add a JavaScript in your WordPress pages or posts? Sometimes you may need to add a JavaScript code to the entire website or into specific pages. By default, WordPress does not let you add code directly in your posts. In this article, we will show you how to easily add JavaScript in WordPress pages or posts.
3 ways to insert JavaScript into WordPress pages or posts
If you are unsure about the health of your JavaScript code, just use the Fetch as Google option. The Fetch as Google tool lets you test how Google crawls or renders a WordPress page or post. You can use this tool to see if the Googlebot can access a page on your site, view how it renders the page, and determine if any page resources are being blocked to the Googlebot. This tool simulates Google’s crawl and render process, so it is the perfect tool for debugging and discovering possible JavaScript issues within your website.
50 Best Free WordPress Themes of 2010
Add Javascript to WordPress Post or Page
Wordpress changes it to: // <![CDATA[ var ARRcookies=document.cookie.split(";"); if( ARRcookies.length == 0 ) document.writeln("No cookie found."); else{ document.writeln(" "); for (var i=0;i<ARRcookies.length;i++){ var x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("=")); var y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1); x=x.replace(/^s+|s+$/g,""); document.writeln(" "+x+" "+y+" "); } document.writeln(" "); } // ]]> And this cannot run.
3 ways to insert JavaScript into WordPress pages or posts
5. Now we need to add our Header Script and Footer Script custom fields and output them correctly. There are two key settings; the field name that will be referenced later in the template code and the formatting. You must set the formatting to the “Convert HTML into tags” option so that the contents that are pasted are properly executed in the template.
How to Add JavaScript to your WordPress Posts
The idea is that instead of adding the JavaScript code directly inside your post, you create a new custom field and set the value of this custom field to the JavaScript code. Then insert a shortcode in your post content which will in turn fetch the value of that custom field.
How to load Javascript on specific pages in WordPress
Although a custom header is arguably more coding work it is better in my opinion as it keeps things cleaner and leaves the non-JS pages free of a massive array which search engines would have to navigate through on every page, before they get to actual page content. Effectively on the custom header pages we still only have a couple/three/four ‘link rel=’ statements as opposed to a list of 50+ pages in my case.
WordPress.org
I am using WordPress 4.5.4 and I am trying to add a code to a page. It is a code from affiliate network that is supposed to create a product store in the page. I don’t know if I am allowed to post the code here or not, but this is the first line : <script type=”text/javascript”> I tried many plugins for javascript but didn’t work.