Maximize Blog reach with Twitter

Automatically tweet new posts without a plugin.

    Inhaltsangabe
  1. 1. Create a developer account
  2. 2. Create a Twitter Apps
  3. 3. Api download
  4. 4. The code

If you have a website like mine, where the main part is that new blog posts are written daily, you have a very limited reach. The only thing you can do is to optimize your website as much as possible in order to be as good as possible in the search results of search engines.

The best strategy to increase traffic, in this case, is to make your posts available on other platforms. The best way to do that is through social media and that’s why I decided to create a post bot for Twitter. I’m going to program a bot that automatically tweets, hashtags and links my daily blogs. That should not be too hard …

1. Create a developer account

To do this, go to the developer page of Twitter and log in. It is also possible to use already existing accounts. After logging in, you will be asked by Twitter what you want to use your developer account for. Here you have to describe with at least 300 characters what you are planning and how you want to realize it.

Then you have to wait until you get a confirmation email that the account is unlocked. In my case it took only half an hour and my account was confirmed.

2. Create a Twitter Apps

On the app overview page you can then create an app , name it and specify a purpose. Once you’ve created the app, you’ll be redirected to the app’s details page. There you have the possibility to generate Api Keys and Tokens. You do not need to write these down, as with some other pages because they are visible on every call. They are not hidden or removed.

As it seems, the tokens are not time-limited. This means that once a token is created, it will be infinitely valid. Nevertheless, one should keep an eye on his emails and see if something is required to keep the tokens.

3. Api download

Yesterday, I looked at the Twitter API documentation page for the best way to use the API, and there were git repositories for almost every use case where you could download APIs to include in your code with a simple require_once . I chose the J7mbo / twitter-api-php and did not feel any performance issues.

4. The code

All you need to do is load the TwitterAPIExchange.php file into your project from this repo and load it with the require_once command. My file structure in my WordPress Theme currently looks like this:

  • inc
    • twitter
      • TwitterAPIExchange.php
      • twitter.php

In the functions.php I’ve added a line:

require_once('inc/twitter/twitter.php');

and the twitter.php contains only the following lines

<?php
function post_to_social_media( $ID, $post ) {
	require_once('TwitterAPIExchange.php');
	$settings = array(
		'oauth_access_token' => "XXXXXXXXXXXXX",
		'oauth_access_token_secret' => "XXXXXXXXXXXX",
		'consumer_key' => "XXXXXXXXXXXX",
		'consumer_secret' => "XXXXXXXXXXXX"
	);
	$twitter = new TwitterAPIExchange($settings);


	$tweet_text = $post->post_excerpt."\n";
	foreach (wp_get_post_tags($post->ID, array('fields' => 'names') ) as $tagName) {
		$tagName = str_replace(
		  array('-', ' '),
		  array('', ''),
		  $tagName
		);
		$tweet_text .= '#'.$tagName.' ';
	}

	$tweet_text .= "\n".get_permalink($post);

	$url = 'https://api.twitter.com/1.1/statuses/update.json';

	$tweet = json_decode( (string) $twitter->buildOauth(
			'https://api.twitter.com/1.1/statuses/update.json',
			'POST'
		)->setPostfields(array(
			'status' => $tweet_text
		))->performRequest()
	);

	if ($tweet->id) {
		update_post_meta( $post->ID, 'tweet_id', $tweet->id );
	}else {
		ob_start();
		var_dump($tweet);
		$tweet_dump = ob_get_contents();
		ob_end_clean();
		wp_mail(
			get_option('admin_email'),
			'Tweet Fail',
			"<pre>$tweet_dump</pre>",
			array('Content-Type: text/html; charset=UTF-8')
		);
	}
}
add_action( 'publish_post', 'post_to_social_media', 10, 2 );

The procedure of this code can be described as follows:

  1. Load TwitterAPIExchange.php.
  2. Access data of the app into $settings.
  3. Generate a TwitterAPIExchange object in the $twitter variable.
  4. Tweet text ( $tweet_text ) starting with the excerpt $ post-> post_excerpt and then generate a new line (\ n)
  5. All keywords of the article should be fetched ( wp_get_post_tags ) and processed individually ( foreach )
    1. Hyphens and spaces are removed.
    2. A rhombus ( # ) built in front of the key word so that it is recognized as a hashtag.
  6. A new line created ( \n ) and then the URL for the post is appended ( get_permalink ($ post) ).
  7. This built-up tweet ( $tweet_text ) should be posted on Twitter and the feedback should be saved as a string ( (string) ) so you make sure every time you access the variable again, you don’t post a tweet.
  8. If a Tweet ID ( $tweet->id ) has been returned, it should be stored in the Post Metas ( update_post_meta ) and if not, the output ( var_dump ) should be sent as an e-mail ( wp_mail ) to the admin ( get_option (‘admin_email ‘) ).

Topics

performance php plugins SEO Social media technology Twitter WordPress

Beitrag teilen

WhatsApp it

Folgen Sie uns