Maximize Blog reach with Facebook

Post published blogposts automatically without a plugin on facebook.

    Inhaltsangabe
  1. State of the day
  2. The php API

After I programmed that my posts are getting automatically tweeted when published, I’ve researched, whether it would make sense to, post his posts automatically on Facebook and then have seen amazing statistics on kontor4.de.

Facebook is used by almost 50 times more than Twitter in Germany. As you can see in the graphic, instagram is even more used but I could not find an api for instagram.

State of the day

A few years ago, when the whole story with Cambridge Analytica had not arrived in the mainstream media, I wrote a plugin to publish my posts on my own Facebook page. At that time you just had to create an facebook app and you could start right away.

Now, when I created a new app on Facebook to automatically post my posts, it was really annoying. I had to:

  • Describe what I want to do with the app.
  • Again for Facebook workers describe what I want to do with it
  • Add a link from Facebook to my website.
  • Record a video of my code and show what it does and send it to Facebook so they can confirm the process.

What was once again is done in the name of justice is a farce. Anyone can lie in the description and write a code, so his app is unlocked. After it’s been unlocked, you can change its source code and have completely different functions.

Because the registration, confirmation and the rights permittings is so complicated, I'll skip this part. 

The php API

Unlike twitter, facebook offers its own Git repository with a full SDK . This can be downloaded directly. If you use Composer , you can follow the instructions in the Readme. However, if you do not use composer (like me), the git repository can be simply downloaded because you only need the files in the facebook folder .

Once you’ve integrated that into your php file with require_once, you can authenticate yourself:

require_once('sdk/autoload.php');
$fb = new \Facebook\Facebook([
	'app_id' => 'XXXXXXXXXX',
	'app_secret' => 'XXXXXXXXXX',
	'default_graph_version' => 'v2.10',
	'default_access_token' => 'XXXXXXXXXX',
]);

After that you can do your queries with the $fb object. In my case the function should just post a published post automatically on facebook. Just like I did before with the twitter API:

The facebook_post function looks like this at the end:

function facebook_post( $args ){
	require_once('sdk/autoload.php');
	$fb = new \Facebook\Facebook([
	  'app_id' => 'XXXXXXXXXX',
	  'app_secret' => 'XXXXXXXXXX',
	  'default_graph_version' => 'v2.10',
	  'default_access_token' => 'XXXXXXXXXX',
	]);



	try {
	  $response = $fb->post('/me/feed',array(
		  'message' => $args['text']."\n".$args['hashtags'],
		  'link' => $args['link']
	  ));
	} catch(\Facebook\Exceptions\FacebookResponseException $e) {
		ob_start();
		var_dump($e);
		$facebook_post_dump = ob_get_contents();
		ob_end_clean();
		wp_mail(
			get_option('admin_email'),
			'Facebook Post Fail',
			"<pre>$facebook_post_dump</pre>",
			array('Content-Type: text/html; charset=UTF-8')
		);
	}

	update_post_meta( $args['post_id'], 'facebook_post_id', json_decode((string) $response->getBody())->id );

}

As you can see, I have declared a function that handles all the informations of a post and posts it to my facebook-page. I’ve added this function to the wordpress publish_post action, so it will run automatically on publishing new blogposts.

My next step is to implement google adsense and look how it works. I’ll see if it makes sense to add ads to your website and will let you know after some experience.

Topics

api automation facebook php Program SEO Social media statistics technology WordPress

Beitrag teilen

WhatsApp it

Folgen Sie uns