Load & Display AddThis Social Sharing Javascript Code From External URL

AddThis is a premium provider of social sharing buttons which can be installed manually or using the AddThis plugin for WordPress.

In this post, i’ll show you how to load the addthis script from an external URL source.

I’ll also show you how to display the buttons in any WordPress or Genesis hook position conditionally using a simple custom function.

addthis before single post content

Note: Any code you use from this post should be copied from the view raw link in the Gist and pasted at the end of your child themes functions file.

Load AddThis Script From External URL

Generally when loading scripts in WordPress, you would use the wp_enqueue_script function. AddThis won’t load if you use this function alone so you will need to use the init hook in its place or with it.


Comments

9 responses to “Load & Display AddThis Social Sharing Javascript Code From External URL”

  1. Leonard Flier Avatar
    Leonard Flier

    As usual, Brad, your thoughts are immediately useful. I’ve relied on you a lot in learning Genesis.

    As a way of giving back, here’s something I was able to accomplish using the ideas from this post, as well as some ideas from the AddThis support site.

    In my site, I am using the AddThis plugin to display the AddThis sharing icons on my posts. But I also have a plugin on my site (CM Answers Pro) in which the AddThis sharing icons were not displaying. This plugin has its own social sharing function, but I wanted AddThis for consistency of appearance.

    Adapting your ideas, I was able to use a hook in CM Answers Pro to get the AddThis sharing icons to display exactly where I wanted them. Here is the code I used in my Genesis theme’s functions.php*:

    https://gist.github.com/laflier/6dbba74fcf3ac821c9f7

    * For anyone who wants to do exactly what I did, the hook for CM Answers is “cma_index_header_after”. The hook only exists in the index.phtml page, but you can manually add it to the single.php page.

    Thanks again for this post, and for everything else you’ve shared. You have no idea how much you have helped me.

    LF

    1. Brad Dalton Avatar
      Brad Dalton

      Thanks for sharing your solution Len.

  2. Brett Atkin Avatar
    Brett Atkin

    Thanks for the samples.

    Got this working for the Blog pages on this beta site:

    (Using the Swank theme)

    Question: On the Gallery page (CPT), the sharing buttons aren’t loading.

    This is the theme’s code for the single-gallery.php page.

    These are posts, something else is wrong.

    Thanks

    1. Brad Dalton Avatar
      Brad Dalton

      There’s 2 steps to loading the buttons.

      1. In genesis, you can add the Javascript to the Genesis > Theme Settings > Header and Footer scripts.
      2. Then you can hook in the div class for the buttons using a conditional tag

      You will need to add one to display the buttons on single page cpt’s.

      Change this line:
      [code]
      if ( is_singular(‘post’) ) {
      [/code]
      To this:
      [code]
      if ( is_singular(array(‘post’, ‘gallery’) ) ) {
      [/code]
      Note the extra closing bracket.

      Where gallery is the name of your Custom Post Type.

      1. Brett Atkin Avatar
        Brett Atkin

        Works, thanks!

        1. Brad Dalton Avatar
          Brad Dalton

          Yes, using it on this site myself.

  3. Hi Brad, AddThis is a great plugin though I find it a pain in the rear to customize the data-via attribute for the Twitter button, meaning that it will show “via @AddThis” by default. Here’s instructions for that if you’re interested: http://www.addthis.com/forum/viewtopic.php?f=3&t=29135

    The easier solution is to use the Digg Digg plugin, which has been taken over by the folks at @bufferapp. It’s very flexible, with lots of great features and well supported too.

    Keep up the great work here!

    1. Thanks for the tip David

      Hard to find a plugin which is always 100%

      I like the tracking which comes with AddThis but have been looking at Digg Digg and might try it sometime on this site.

      Have fun at the Party!

      Party

      This year’s WordPress party will be hosted by the WordPress Foundation on Monday, March 12 from 6-9pm. Space is limited, so make sure you RSVP (no SXSW badge is required). The party this year will be at the Buzzmedia Pure Volume House, and the story of how we hooked up with them is pretty cool.

      Once upon a time, David Wang had a business called Buzzmedia in Malaysia, with the twitter username @buzzmedia. When David changed gears and started ClickWP, a WordPress support business, he stopped going by the Buzzmedia name. In the U.S., a company also called Buzzmedia wished it had that Twitter username, and asked if they could have it since David wasn’t going to use it anymore.

      David, feeling the WordPress community love, said he would give them the name, and suggested they do something in return for the WordPress Foundation. So, everyone talked to everyone else and it worked out that Buzzmedia was willing to donate a fantastic venue for this year’s party as well as covering the bar.

      In the end, the Foundation got a great SXSW party, Buzzmedia got their twitter username, and David got the warm glow of having used his power for the good of the WordPress community, and they all lived happily ever after.

      Seriously, though, the PureVolume House is always a great SXSW venue, so thank you David and Buzzmedia for your generosity! We’ll have drinks and snacks and a few hundred WordPress-loving partygoers, so you know it will be a good time. Kind of like a WordCamp afterparty without all the work of a WordCamp.

Leave a Reply

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.