Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    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.