[Resolved] Using custom JavaScript in a page

Home Forums Support [Resolved] Using custom JavaScript in a page

Home Forums Support Using custom JavaScript in a page

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #186082
    Ryan

    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.

    #186101
    Tom
    Lead Developer
    Lead Developer

    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; ?>
    #186106
    Ryan

    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!

    #186237
    Tom
    Lead Developer
    Lead Developer
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.