Title: How to add javascript code
Last modified: February 22, 2019

---

# How to add javascript code

 *  [protovest1](https://wordpress.org/support/users/protovest1/)
 * (@protovest1)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/how-to-add-javascript-code/)
 * Hello,
 * I would like to add a javascript code to this page only for the dryers.
    [https://protovest.com/products/dryers-2/](https://protovest.com/products/dryers-2/)
 * It didn’t work when I pasted it in the html side so I’m assuming that it belongs
   somewhere in one of the php files?
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fhow-to-add-javascript-code%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 1 replies (of 1 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/how-to-add-javascript-code/#post-11238073)
 * Yeah, the WP post editor has a nasty habit of corrupting any code added into 
   it.
 * You have a few options for adding uncorrupted code. You could modify a theme 
   template to output the code directly. Naming a template single-product-dryers-
   2.php should cause it to only be used for that one product page.
 * You can create a custom shortcode that returns the code within script tags, then
   include the shortcode in page content. Shortcode return is spared the corrupting
   influence of the editor.
 * You can output inline script from an action hook like wp_head, wp_footer, or 
   wp_print_scripts. These fire for all pages, so you need extra code to check the
   queried object and only output script when the correct object is queried.
 * Those methods are better for fairly short scripts. The official WP way of adding
   scripts is meant for more extensive script in external files. You use wp_enqueue_script()
   called within a callback hooked to “wp_enqueue_scripts” to tell WP what file 
   you want referenced in the head or footer sections. You also tell WP which other
   scripts yours is dependent on, such as jquery, and WP makes sure the files are
   loaded in the correct order. Enqueued script also is applied to all pages unless
   you add conditional code that checks the queried object.

Viewing 1 replies (of 1 total)

The topic ‘How to add javascript code’ is closed to new replies.

## Tags

 * [javascript](https://wordpress.org/support/topic-tag/javascript/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [7 years, 1 month ago](https://wordpress.org/support/topic/how-to-add-javascript-code/#post-11238073)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
