I’ve got a simple requirement, but can’t for the life of me figure out how to do it: I’d like to allow specific Twitter users to view a WordPress blog. These users are all followers of a [locked] Twitter account.
I presume I’d need to use oAuth but all plugins and tutorials I’ve found are generic – ie you login with Twitter, and that’s it. I’d like to go an additional step and check that the user is following the locked account too.
Doable?
Create a Twitter app associated with your protected account. (I would give you the link, but I don’t have enough SO points to have more then 2 links in a post. So just Google this step)
Create a custom WordPress page template for your protected pages. See http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates
The following steps should be placed inside your page template code:
Once the user is logged in using your Twitter OAuth plugin that you mention, find the current user’s Twitter ID, it will almost certainly be kept in the $_SESSION global somewhere.
Use a PHP wrapper to the Twitter API (like this one https://github.com/abraham/twitteroauth/blob/master/DOCUMENTATION) to call the Twitter API’s
GET /friends/ids
method with the screen_name set to your Twitter handle. You will need the CONSUMER KEY and SECRET given to you in step 1. This will give you a list of your followers.Check if the logged in user’s id is in the list of user ids returned by the previous step. If so then call
the_content()
to render the pages content, if not then don’t make that call.Sacrifice 3 grain-fed male sheep, 2 creatures of the night and 1 black, legless insect.