Conditionally load external JS per post basis?

Community Forums Forums Design Tips and Tricks Conditionally load external JS per post basis?

This topic is: resolved

This topic contains 2 replies, has 2 voices, and was last updated by  lvvvvvl 2 years, 1 month ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
  • #67267



    I’m trying to load this 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 PROFILE ID HERE]) .

    Any advice will be much appreciated!


    Gary Jones

    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   <a href="" rel="nofollow"></a>
     * @return null Return early if not a single entry.
    function prefix_enqueue_single_scripts() {
    	if ( ! is_single() )
    	wp_enqueue_script( &#8216;prefix-add-this&#8217;, &#8216;;, array(), &#8216;1.0.0&#8217;, true );

    Changes in Genesis 2.1 – the ultimate guide to every single change in Genesis Framework 2.1, 2.1.1 and 2.1.2 (all 90 of them!) | @GaryJ



    Awesome! thank you!

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Conditionally load external JS per post basis?’ is closed to new replies.