• Here’s the situation I’m having. I am developing a site and on the single post pages of the site I am using various social sharing buttons (i.e. Google+, Facebook, LinkedIn, Twitter, etc). I am following the official developer documentation for each of these buttons and most of them require inserting JavaScript before the closing </body> tag.

    This is all fine and dandy and I’ve got it working perfectly. However, my only issue is that since these social icons are ONLY on the single post blog pages I don’t want that JavaScript being loaded on other pages as it will only add unnecessary resources to be loaded and I’d like to keep my page speed as fast as possible.

    For the life of me I can’t figure out how to go about doing this. I thought at first about writing some conditional PHP code that detected if you were on a single post page and if so it loaded the scripts in the footer. However, I realized that I don’t think there’s a realistic way to do that since each single post URL doesn’t have a consistent part of the URL in it (i.e. /blog/).

    I could add the JavaScripts to the single.php template for single post pages but that wouldn’t work either since it’s calling the footer in those pages via <?php get_footer(); ?> and I would like these scripts to be loaded in the footer, not the template itself.

    Anyways, any input would be appreciated. I’m sure there is a way to do it, I’m just racking my brain and can’t figure it out.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Are you enqueueing this javascript correctly via your theme’s functions.php file?

    Thread Starter digitalsky

    (@digitalsky)

    I’m not sure if I need to do that since one of them is just an inline JavaScript snippet that looks like this…

    <script type="text/javascript">
    			(function(d){
    				var f = d.getElementsByTagName('SCRIPT')[0], p = d.createElement('SCRIPT');
    				p.type = 'text/javascript';
    				p.async = true;
    				p.src = '//assets.pinterest.com/js/pinit.js';
    				f.parentNode.insertBefore(p, f);
    			}(document));
    		</script>

    And the other looks like this…

    <script src="//platform.linkedin.com/in.js" type="text/javascript">lang: en_US</script>

    Does it still need to be enqueued if it’s not actually a JavaScript file?

    Does it still need to be enqueued if it’s not actually a JavaScript file?

    Yes. It still is a .js file. Just not one hosted on your server.

    Thread Starter digitalsky

    (@digitalsky)

    Gotcha, I wasn’t aware of that being that I’m more of a ‘designer’.

    I added the majority of the social sharing buttons to one .js file I created here…

    http://198.1.77.196/~larog/wp-content/themes/larog/js/social.js

    That should clean things up by allowing me to call on one file instead of a bunch.

    I’ll enqueue that in my functions.php file and it just dawned on my I know how to make this code only appear on the single post pages by using this code…

    <?php if ( is_single() ) {
    	echo
    		'<script type="text/javascript" src="/~larog/wp-content/themes/larog/js/social.js"></script>'
    	. single_cat_title() . ' category!';
    }
    ?>

    Looks like I answered my own question (but thanks for the enqueue reminder, I thought you only had to do that on .js files hosted on my own server). Sometimes I have moments where the obvious solution will easily elude me.

    Thread Starter digitalsky

    (@digitalsky)

    Of course, I spoke to soon. Here’s the issue now. I’m enqueueing the script in my functions.php file like this…

    // Add our own JavaScripts.
    function custom_scripts()
    {
    	// Register the scripts
    	wp_register_script( 'modernizr', get_template_directory_uri() . '/js/modernizr-2.6.2.min.js', array(), '2.6.2', false );
    	wp_register_script( 'formalize', get_template_directory_uri() . '/js/jquery.formalize.min.js', array(), '1.0', false );
    	wp_register_script( 'social', get_template_directory_uri() . '/js/social.js', array(), '1.0', true );
    
    	// Enqueue the scripts:
    	wp_enqueue_script( 'modernizr' );
    	wp_enqueue_script( 'formalize' );
    	wp_enqueue_script( 'social' );
    }
    add_action( 'wp_enqueue_scripts', 'custom_scripts' );

    Note that the script I’m trying to work with here is the ‘social’ one in the code above.

    Now the problem I am having is that when I enqueue that code it works great and shows up in the footer but now of course I can’t use that code from my previous post that detects if it’s a single post page and then displays that code. The enqueue code I am using by default just inserts the script in the header or footer (whichever you tell it to).

    Thread Starter digitalsky

    (@digitalsky)

    I figured it out. This is the code that did the trick (this goes in the function.php file)…

    // Add our own JavaScripts.
    function custom_scripts()
    {
    	// Register the scripts
    	wp_register_script( 'modernizr', get_template_directory_uri() . '/js/modernizr-2.6.2.min.js', array(), '2.6.2', false );
    	wp_register_script( 'formalize', get_template_directory_uri() . '/js/jquery.formalize.min.js', array(), '1.0', false );
    	wp_register_script( 'social', get_template_directory_uri() . '/js/social.js', array(), '1.0', true );
    
    	// Enqueue the scripts:
    	wp_enqueue_script( 'modernizr' );
    	wp_enqueue_script( 'formalize' );
    
    	// Only enqueue the social script if we're on a 'single' post page (we don't want this script being loaded on pages without the social sharing icons):
    	if ( is_single() ) {
    		wp_enqueue_script( 'social' );
    		echo '<script src="//platform.linkedin.com/in.js" type="text/javascript">lang: en_US</script>';
    	}
    }
    add_action( 'wp_enqueue_scripts', 'custom_scripts' );

    That code enqueues all of my scripts I’ve added on. It also detects whether the visitor is on a single post page and if so it loads the social.js script (which controls all my social media sharing buttons) in the footer of the site. Works like an absolute charm and ensures that these scripts are not being loaded on pages that don’t need it. This is a nice fix for anyone who is concerned about page speed.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How can I load certain JavaScripts only on blog pages?’ is closed to new replies.