Site logo

Archived topic

Using custom JavaScript in a page

3 replies · Started by Ryan on April 11, 2016

Viewing posts 1–4 of 4

Hi Tom,

Is there and easy recommended way to insert custom JS into a page?
I am pretty sure I can find a third party plugin to do the job but I'd like to find a recommended approach.

I only want the Javascript to run on a particular page so hence why I don't want to insert it into a PHP template. What I also could do is make a custom template PHP file based from the default and put the code in there and then use it as the pages's template but that just seems like a significant amount of work for a few lines of JS script.

You could use GP Hooks in the wp_footer hook:

<?php if ( is_page( 'my-page-slug' ) ) : ?>
    <script>
        // Your script in here
    </script>
<?php endif; ?>

Ok cool, so I can conditionally target the page using some PHP logic: <?php if ( is_page( 'my-page-slug' ) ) : ?>

I already had it in the WP Head of the hooks section (it needs to load an audio loop on startup). Just tested out the slug conditional and it works a treat.

Thanks again!

This archived topic is closed to new replies.