Table of Contents
As we already discussed, if you try to copy-paste a code snippet into wordpress, it will automatically get deleted when you save it. Instead, you have to use some kind of tool or a plugin to accomplish the task.
And that is the reason why you have to use a tool or plugin to add your JS code snippet so that you don’t have to install it again and again on your site every time you update or make changes to it. In this section, we will be using WPCode to add the JS code snippet. It is one of the powerful code snippet plugins for wordpress that enables you to customize the code from any section of your website without charging a single penny.
First of all you have to download and activate your WPcode plugin. Once you have activated WPCode, visit Code Snippets » Headers & Footer. There you can see three different fields namely header, body and footer.
Once you have selected that, scroll down in the create your own snippet to find out the ‘Insertion’ section. From there, you have to pick out a location to place your code on the website from the dropdown menu. You will be provided with several options categorized in ‘Page, Post, Custom Post Type’ to select where in the web page or the post would you like your code snippet to appear.
Now that you are using the WPCode to insert the code snippet, you have to add it before or after the paragraph. For that, you will be able to choose specifically after or before which paragraph in the post you want your JS code snippet to appear. For example, if you add the number 1 in the field called ‘Insert Number’ then your JS code will appear before or after the first paragraph of the post. Same goes for number 2 and so on.
Once that’s taken care of, all you have to do is click on the toggle that is located on the top of your screen to switch to ‘Active’ and then you have to click on the ‘Save Snippet’ button that lies right beside it.
Now if you add the above code then your website will only run it if the post ID matches with the ‘5’. Now, of course, it goes without saying that you have to replace the ‘5’ with the post ID for which you want this code to be active. If you don’t know the post ID then it’s easy to check out. Just open up the post where you want to run your JS code and there you will find the post ID in the URL of the page.
As in the case of posts, the above code will run only on the page with page ID mentioned in the code. So you have to mention the page ID in the place of ‘10’ in this code. You can find the page ID for the web page where you want to run code snippets by looking at its page URL.