How to Create a WordPress Reward for Tweet Plugin

Facebook

Twitter

Google+

Pinterest

+

As a website owner or blogger you need a good number of subscribers and followers to be successful in the social media world. Many websites provide freebies such as eBooks, Icon Packs and Plugins in exchange for emails. Getting people to tweet your content is another trending method to get more visitors to your site. In this tutorial, I’ll be showing how to create a WordPress reward for Tweet plugin to increase the tweets on your content while satisfying everyone!

Basically we are going to provide rewards for users who tweet our content. You will be able to provide a freebie for each of your blog posts or tutorials. Once the user tweets the content, the download link will be shown to the users. You can provide related links, tutorial files, link for a special offer as a reward, and so on.

Creating the Plugin Files

First thing we have to do is create a folder called Reward-Tweets in the wp-content/plugins directory and create the index.php file inside the folder. Once it is done we need to provide the information about the plugin on the top of the index.php file using comments as shown in the code below.

Now you will be able to see the plugin on your admin dashboard with the link to activate the plugin.

Adding Reward for Tweet Link to Posts

Each post is going to have its own reward link. You can omit the reward link if you don’t have anything to provide for a specific post. So first thing we need to do is to add a textbox to the post creation screen to insert reward links.

Adding Meta Boxes to Post Screen

We can add custom fields using WordPress meta boxes. Consider the code below.

Initially we get all the custom meta values related to the current post using get_post_custom function.

Then we check whether Reward Link exists for the current post using the received post meta values. If a link is already available we insert it as the value of text field.

Next we create a WordPress nonce value using wp_nonce_field function. This will be used to validate the request when we hit the publish button. Provide a unique key for the parameters of this function.

Finally we display the HTML label and text field for our Reward Link.

If you configured everything correctly up to now, you should be able to see the meta box with Reward Link field in the bottom of the post creation screen after activation.

Saving Reward Link Value to Database

Now we have to save the value of the Reward Link once you hit the publish button and save the post. Following code will be used to save the data to wp_postmeta table.

Initially we call the add_reward_tweet_button function on the_content filter. This filter will be applied for content of every post and page.

Page or post content will be automatically passed to this function.

Then we get the custom meta values of the current post.

We don’t want to include this Reward Link in summary, category, archive, tag pages. We call the function is_single to ensure that this is the detailed post page.

Also we check whether Reward Link exists for current post and display the tweet button.

Reward Panel will not be visible if the post does not have a reward link.

Now you should be able to see the Reward Panel under each post with a reward link as shown in the screen below.

Provide Download Links On Tweet

Now once the tweet button is clicked and tweet is published on user’s profile, we need to display the Reward Link. We are going to use Twitter Web Intents Events to identify the end of a tweet. You can find more information about Twitter events on the Twitter Dev center. So let’s create the JavaScript code in the rewardTweet.js file to handle the Reward Link display after tweet is published.

First we use the action defined in previous section and configure the AJAX requests handling function using wp_ajax_nopriv_get_reward_links and wp_ajax_get_reward_links.

wp_ajax_nopriv is used for users who are not logged into the site.

wp_ajax is used for users who are logged into the site.

Inside the get_reward_links function we get the reward link for the current post and sends it back to browser with status in JSON encoded object.

Previous JavaScript code will decode the JSON object and display the Reward Links for the user.

Now we have the completed Reward for Tweet plugin. You can easily insert rewards for each post in the admin dashboard and allow users to download once the tweet is completed. We hope this plugin helps you promote your website and gain newer visitors!

Facebook

Twitter

Google+

Pinterest

+

The Community of Web Design Professionals

"It's our personal mission to make sure you become the best web design professional." James, CEO / Co-Founder

Valuable ideas once a week

AwesomeWeb is Awesome!

Andreas W.

Submitted 9:02 AM .Oct 16, 2014

Hi Nick. If you have work that needs to be done, send me an email. I am glad to help out. Awesomeweb already helped me to earn an awesome 3,700 dollars since the start.;) ... yeah farewell Elance!

Comments

But wordPress already have plenty of small plugins that does exactly this + more, why go to the extend of doing all coding when you can simply click a button and install it from the WordPress repository ?

Hello Rakhitha,
The pluging is working now, I downloaded the plugin from the demo link you provided in the post, I think I did something wrong while creating the pluging by my own, anyway thanks for this awesome giveway,
I wanted to ask you one more thing, how can I make the tweet to get reward section in the post to appear on top section of the post , the tweet to get reward is at the bottom of every post,
please let me know I am looking forward to your reply
thanks

The plugin not working. :( I am seeing this message after activating it.The plugin generated 259 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.
please tell me what wrong I am doing, I really need this plugin working for my blog.

I cannot find any issues since its working fine on the demo site. Can u please send a screenshot of what you get ?
If possible check this plugin on a fresh WordPress installation. Some other plugins which you have already installed might have something to do with this error.