Auto-posting WordPress posts to Twitter without plugins
How to automatically post WordPress posts to Twitter without the use of any plugins. This allows for excellent customisability and can be tailored to meet the requirements of any client.
With social media seemingly taking over the world, it is often essential that a website can effectively contribute to client’s social accounts. This generally comes in the form of having new posts being automatically posted to Twitter or Facebook. In this blog I am going to walk through how to auto-post to Twitter without the use of any plugins in WordPress.
There are many plugins which can ultimately achieve this goal, but this unnecessarily adds to the number of installed plugins, and I find many to be rigid. For example, if you want only certain posts being submitted, or if you want to give users the choice to post to separate Twitter accounts, there aren’t many (none that I seen) plugins providing this level of customisability.
Twitter provides a great API to work with. For PHP there are many available libraries to interact with Twitter. I tend to go with Codebird for general ease of use and a great feature-set. There are a few server requirements when using this library, but any decent hosting plan should have these covered.
The first step is to download the library. Go to the Codebird Github repository, switch to the Release branch and click the “Clone or download” button. You can use Git to clone the repository to your system, or download the zip and extract it, up to you; the end-result will be you having the folder on your system.
Rename the folder to “codebird” and move it to your WordPress theme folder. I usually add libraries to an “includes” folder:
The “codebird” folder should now contain the “src” and “test” folders, along with the other files from the download. There is only really one file needed here (src/codebird.php), but this is up to you.
Create Twitter App
The next step is to create a Twitter app here. You may need to register if you haven’t already. Creating an App will allow us to authenticate the current Twitter account and enable us to Tweet from it.
Click the “Create New App” button and add the relevant details for your app:
After the app has been created, go to the “Keys and Access Tokens” tab, everything else can be ignored for now. Click “Create my Access Token”. Take note of the 4 unique keys here:
- Consumer Key (API Key)
- Consumer Secret (API Secret)
- Access Token
- Access Token Secret
Read and write access should be enabled for default, so leave these as they are.
One thing to be very careful of when copying these keys is that there may be extra spaces in them. This is very easily missed when copying from Twitter, so i’d suggest pasting them into Notepad or similar to strip any extra spaces. Also, do not store these keys in a public repository.
It’s now time to get stuck into the actual code to get this working.
I’ll explain what the code is doing before showing it. I’m using the WordPress hook transition_post_hook to check the current status of a post (if it’s a draft for example), and only firing the auto-post function when it changes to a published post.
Open your functions file (create this if it doesn’t already exist):
Add the following:
Now it’s time to create our function. This is named ‘twitter_auto_post’.
The first action of the function is to grab the ID of the posted article. This will allow us to check the post status and get the information for the Tweet.
Next, we check our database to see if the article has been Tweeted previously using get_post_meta. If it has been previously posted, we will not post it again. This is being checked against a value we will add further into the function, so don’t worry about this too much for now:
Now that we have determined that the article has not been previously Tweeted, we require the Codebird library.
Replace the keys below with those previously created from the Twitter Application, allowing Codebird to be setup for the Tweet:
The next step is to actually compose what is to be Tweeted. This can be easily customised, but for this example i’ve went with a simple “New post! Post title – Post link” and an image.
The image section can be left as it is, as this generally never changes so isn’t anything to worry about:
…and finally! It’s time to put the tweet together and send it to Twitter. This comes in the form of an array which contains our previously created status and the post image:
The final piece of the puzzle is to log if the Tweet was successful or not. This is important as it will prevent duplicate Tweets. It will also log any errors that may have occurred, allowing easy debugging of failed Tweets (I have yet to see any fail with this function). There will be a meta key stored in the database in the ‘wp_postmeta’ table with the key ‘social_twitter_posted’, with either the value ‘true’ or the error received.
Go check your Twitter account and you should see a shiny new Tweet! It should contain the title, link and image of the post which was just published in WordPress. See the image below for an example of this:
Full example below:
Codebird serves as an excellent library for effectively auto-posting WordPress posts to Twitter. This function can also be customised in any number of ways, allowing great flexibility for any client’s needs. If you have any questions or issues feel free to get in contact with me 🙂