• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Add javascript to template in Genesis

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › General Discussion › Add javascript to template in Genesis

This topic is: not resolved

Tagged: genesis, javascript, template

  • This topic has 3 replies, 2 voices, and was last updated 10 years, 11 months ago by Ren Ventura.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • August 11, 2014 at 11:05 am #118221
    van224
    Member

    Hi everyone

    I need to add a piece of javascript code to all of the pages that use a particular php template. My site is running on the Genesis framework. Any idea how to do this?

    Many thanks in advance
    V

    http://adrenalfatiguesolution.com
    August 11, 2014 at 12:30 pm #118244
    Ren Ventura
    Member

    Enqueue it using the is_page_template() conditional tag.

    http://codex.wordpress.org/Function_Reference/is_page_template


    Web & Software Developer & Blogger | RenVentura.com | Follow Me on Twitter @CLE_Ren

    August 11, 2014 at 9:39 pm #118322
    van224
    Member

    Awesome, thanks Ren!

    Will this code work if I just add it to functions.php?? I only want the javascript to be added to my template 'temp1.php'.

    if ( is_page_template( 'temp1.php' ) ) {
    <script type="text/javascript">SCRIPT GOES HERE</script>
    }
    else {
    }

    August 11, 2014 at 10:21 pm #118324
    Ren Ventura
    Member

    Using the enqueue method is the recommended way. If you need more information on the wp_enqueue_script method, visit this link:

    http://codex.wordpress.org/Function_Reference/wp_enqueue_script

    Wrapping the wp_enqueue_script() in the is_page_template() conditional tag will give you what you want. This means that the script will only be loaded if the temp1.php page template is being displayed. Your code should look something like:

    
    add_action( 'wp_enqueue_scripts', 'my_custom_scripts' );
    function my_custom_scripts() {
    	if ( is_page_template( 'temp1.php' ) ) {
    		wp_enqueue_script( 'script-handle', get_stylesheet_directory_uri() . '/js/example.js', array(), '1.0.0' );
    	}
    }
    

    Note that this would add the script to the header of your site. If you want to add it to the footer, you can add the fifth parameter of true to the wp_enqueue_script function.


    Web & Software Developer & Blogger | RenVentura.com | Follow Me on Twitter @CLE_Ren

  • Author
    Posts
Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘General Discussion’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2025 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble