• 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

Conditionally load external JS per post basis?

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 › Design Tips and Tricks › Conditionally load external JS per post basis?

This topic is: resolved

Tagged: enqueue, javascript

  • This topic has 2 replies, 2 voices, and was last updated 12 years, 11 months ago by lvvvvvl.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • October 17, 2013 at 11:43 pm #67267
    lvvvvvl
    Participant

    Hello,

    I'm trying to load this http://www.addthis.com/labs/photo-sharing#.UmC11FBmh8E script per single post basis rather than activate the addthis plugin throughout the whole site.

    I know this must be possible with wp_enqueue_script() somehow and an IF IS_Single conditional statement but the tutorials I've found online only deal with plugins that are already registered, and how to deregister them and then load the .js file inside the plugin folder. There's none in particular about external javascripts (like http://s7.addthis.com/js/300/addthis_widget.js#pubid=%5BYOUR PROFILE ID HERE]) .

    Any advice will be much appreciated!

    October 18, 2013 at 1:29 am #67273
    Gary Jones
    Member

    The source of the script is irrespective - if you want to load a script from a local plugin, or from an external URL, the only thing that changes in the wp_enqueue_script() call is the second argument - the URI path to the script. Try tweaking and adding the following to your child theme functions.php file.

    add_action( 'wp_enqueue_scripts', 'prefix_enqueue_single_scripts' );
    /**
     * Enqueue scripts for this theme that should only be loaded on single entries.
     *
     * @author Gary Jones
     * @link   http://www.studiopress.community/topic/conditionally-load-external-js-per-post-basis/
     *
     * @return null Return early if not a single entry.
     */
    function prefix_enqueue_single_scripts() {
    	if ( ! is_single() )
    		return;
    	wp_enqueue_script( 'prefix-add-this', 'http://s7.addthis.com/js/300/addthis_widget.js#pubid=YOURPROFILEID', array(), '1.0.0', true );
    }
    

    WordPress Engineer, and key contributor the Genesis Framework | @GaryJ

    October 18, 2013 at 1:44 am #67278
    lvvvvvl
    Participant

    Awesome! thank you!

  • Author
    Posts
Viewing 3 posts - 1 through 3 (of 3 total)
  • The topic ‘Conditionally load external JS per post basis?’ is closed to new replies.

CTA

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

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2026 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