0

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.

0

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.

0

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' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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