WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to embed a social sharing button in the theme. Following is the standard code to embed the twitter button.

<a href="https://twitter.com/share" class="twitter-share-button">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>

However, WordPress recommends to use the wp_enqueue_script to include any kind of javascript code. So I am wondering how can I include the above script code properly in a WordPress theme? Thanks.

share|improve this question
up vote 0 down vote accepted

Put everything between <script> & </script> in a separate file. Then use wp_enqueue_script with the correct path of the file & dependencies to false. After that you can use only <a href="https://twitter.com/share" class="twitter-share-button">Tweet</a> to add the tweet button.

share|improve this answer

All that script does is load in another script, and ensures it's only loaded once per-page.

If this theme is just for yourself, and you're not using other twitter-related plugins, I would just enqueue the script directly & skip the "once" checking.

wp_enqueue_script( 'twitter-widget', 'http://platform.twitter.com/widgets.js' );
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.