Make an Auto Twitter Post WordPress Plugin
Are you looking to automatically share your new WordPress posts on Twitter? With just a few simple steps, you can create a WordPress plugin that does the job for you.
First, let’s start by creating a new plugin. Either use FTP or go to your web host’s file manager, go to $HOME/public_html/wp-content/plugins and create a new folder named after your plugin.
Next, we’ll need to create a function that will post our new WordPress posts to Twitter. To do this, we’ll use the Twitter API and the WordPress HTTP API. First, go to the Twitter Developer Portal and create a new developer account if you don’t already have one. Then, create a new app and generate the necessary API keys and secrets.
Now, you will need to upload the ‘TwitterAPIExchange.php’ file to the plugin directory. You can find it here.
Now, let’s create a function that will post to Twitter using the API keys and secrets. Create a new PHP file, and add the following code:
<?php
/**
* Plugin Name: Auto Twitter Post Plugin
* Plugin URI: http://www.mywebsite.com/my-first-plugin
* Description: Automatically posts new WordPress posts to Twitter
* Version: 1.0
* Author: Your Name
* Author URI: http://www.mywebsite.com
**/
require_once 'TwitterAPIExchange.php';
function post_to_twitter($post_id) {
// Get the post and its title and permalink
$post = get_post($post_id);
$title = $post->post_title;
$permalink = get_permalink($post_id);
// Replace YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET, YOUR_ACCESS_TOKEN, and YOUR_ACCESS_TOKEN_SECRET with your own API keys and secrets
$consumer_key = 'YOUR_CONSUMER_KEY';
$consumer_secret = 'YOUR_CONSUMER_SECRET';
$access_token = 'YOUR_ACCESS_TOKEN';
$access_token_secret = 'YOUR_ACCESS_TOKEN_SECRET';
// Authenticate with the Twitter API
$twitter_api = new TwitterAPIExchange(array(
'consumer_key' => $consumer_key,
'consumer_secret' => $consumer_secret,
'oauth_access_token' => $access_token,
'oauth_access_token_secret' => $access_token_secret
));
// Construct the tweet text
$status = $title . ' ' . $permalink;
// Post the tweet
$response = $twitter_api->buildOauth('https://api.twitter.com/2/tweets', 'POST')
->setPostfields(array(
'status' => $status
))
->performRequest();
// Check for errors
if ($response === false) {
// An error occurred, log the error message
error_log('Twitter API request failed');
} else {
// The request was successful, parse the response
$response_obj = json_decode($response);
// ...
}
}
This function will take the title and permalink of the new post and use the Twitter API to post it to your Twitter account. Make sure to replace the placeholder API keys and secrets with your own.
Finally, we’ll need to hook this function to the “publish_post” action in WordPress. This will cause the function to run every time a new post is published. Add the following code to your plugin:
add_action('publish_post', 'post_to_twitter');
That’s it! With just a few lines of code, you’ve created a WordPress plugin that will automatically share your new posts on Twitter. Make sure to activate your plugin in the WordPress dashboard, and you’ll be all set.
I hope this tutorial has been helpful in showing you how to create a plugin that posts new posts to Twitter. For more tips and tricks on WordPress plugin development, be sure to check out our other articles. Happy coding!
If you are in need of web hosting, VPS or domain registration services, we would recommend NameHero.
If you are interested in web design, managed hosting, or marketing services, we offer them at Sunshine Tech and Media.