Blog

Pro Tip: Using an Addon to Filter Video Types

AutoBlogged Addons

While AutoBlogged is a powerful autoblogging plugin, it doesn’t have nearly as many features as we would like to put in to it. Fortunately, AutoBlogged makes use of the WordPress API that allows anyone to expand on AutoBlogged’s features. Essentially, you can write plugins for our plugin.

AutoBlogged exposes a number of actions and filters that allow a programmer to interact with AutoBlogged while it is processing posts. The most useful of these is the before_autoblogged_post filter which allows you to view and edit all post details right before AutoBlogged creates the post. This filter allows you to do enhanced post filtering, pre-processing, or add additional post details.

To assist others in getting started creating addons, we have created a GitHub repository of examples that anyone can contribute to.

An AutoBlogged addon is basically a normal WordPress plugin that interacts only with the events that AutoBlogged exposes. The plugin is very simple that can be demonstrated with the following minimal template:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php

if (!class_exists(' ab_addon')) {
 class  ab_addon {

  // Constructor
  function __construct(){
   add_filter('before_autoblogged_post', array(&$this,'ab_filter_action'));
  }

  // Callback function
  function ab_filter_action($postinfo) {

   // Custom filtering code goes here
   //
   //

   return $postinfo;
  }
 }
}

// Init the class
if (class_exists(' ab_addon')) {
 $ab_skip_post = new  ab_addon();
}

?>

To make this plugin work, all you need to do is add your own custom filtering code, save it in the /wp-content/plugins folder, and activate it in WordPress.

Building A Plugin

To give a real-world example, we recently had a customer request the ability to only include video attachments that are MP4 or M4V formats. The feed they were using had multiple video formats attached, but the site’s video player was only capable of playing MP4 or M4V videos.

This is a perfect case for an addon, because it is not something we would integrate into the main product, but can be accomplished very easily as a plugin.

In this case we used the following code in the ab_filter_action callback function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function ab_filter_action($postinfo) {

 // Loop through each attached video
 foreach ($postinfo['video_urls'] as $video) {
  $ext = strtolower(substr($video, -3, 3));

  // Only allow mp4 and m4v
  if ($ext == 'mp4' || $ext == 'm4v') {
   $video_urls[] = $video;
  }
 }
 
 if (count($video_urls[])) {
  $postinfo['video_urls'] = $video_urls;
 }
 
 return $postinfo;
}

The above code is really very simple, in line 4 we loop through each video, then on the next line extract the file extension and save it to the $ext variable. Finally, in line 8 we check to see if the extension is either mp4 or m4v.

Once we know that the video is one that we want, we add that to a new video_urls array in line 9. Note that depending on how you used this you may also want to create the videos which would contain the actual embed codes.

Finally, the most important step here is to return the modified $postinfo variable back to AutoBlogged. You must always return the $postinfo variable, even if you have not modified it.

Once you upload and activate this plugin, it will be called every time AutoBlogged is ready to add a post. This code will go through and extract only the MP4 and M4V videos and return the modified post info that AutoBlogged will use when creating the post.

Our API is an enormously powerful feature that lets you expand on AutoBlogged an endless number of ways. For more examples on creating addons, be sure to visit our Github repository.

Comments are closed on this post.

Recent Tweets

    No public Twitter messages.


Delicious Save this on Delicious


@AutoBlogged
Find us on Google+