WordPress Tutorial

How to Add Twitter Tweet Button to WordPress

Introduction

Twitter is one of the largest social media platforms. This tutorial shows you how to add Twitter button to WordPress website and allow your visitors to share content from your website to Twitter. We will be adding the tweet button on every post in WordPress.

What you’ll need

Before you begin this guide you’ll need the following:

Step 1 — Opening your post page template

Navigate to your WordPress Admin Area and open up Editor under Appearance:

Opening WordPress editor from appearance

After that, open up a template file for single posts. It is called single.php:

Open WordPress single post template file

Step 2 — Updating post template file

In your single.php file find a line <?php get_template_part( 'content', 'single' ); ?> and after it add the following code:

<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script> <a href="http://twitter.com/share?url=<?php echo urlencode(get_permalink($post->ID)); ?>&via=XXXXXX&count=horizontal" class="twitter-share-button">Tweet</a>

where XXXXXX is your Twitter username:

Modyfying the WordPress post template file

Don’t forget to save changes by clicking Update File.

Step 3 — Testing the Tweet button

Open up any post on your WordPress website and you will see a Twitter button at the end:

Testing Twitter button on wordpress

Conclusion

Now you know how to add a Twitter button to WordPress and it lets your visitors share your content. Just like a button can be added on posts, it can also be added on pages or in sidebar.

About the author

Gediminas B.

Gediminas is a passionate content writer and SEO geek at Hostinger. He started his journey at customer support and aims to use his gathered knowledge to create the most epic content the world has ever seen. From WordPress to server management, there's no topic he can't cover! He also likes cats.

Add Comment

Click here to post a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

More in WordPress Tutorial
How to Add PHP Code to WordPress Post or Page

Close