Custom filter in Feedwordpress Advanced Filters

 

I already posted earlier that Feedwordpress Advanced Filters now supports the possibility to add your own custom filters.

One of the core features of FAF has always been to be able to easily extend the feature set. With the most recent version it’s easy to drop your own filter into FAF. This post will also serve as the beginning of some public documentation.

Warning! After this it will get a bit technical

FAFFilter Class

  
public static $name = "remove_html"; 
public static $description = "Remove HTML from post"; 
public static $context = "posts";
protected static $controls = "search_title, search_excerpt, search_content, html_allow_styles, html_allow_images,html_allow_links,html_allow_custom"; 

static void getControls ()
static void setControls ()
fafFilter __construct ( $post,  $args)
void display ()
Array execute ()

This is what a basic filter looks like.

The class properties define name, description (‘nice name’ – this will be used in the interface), context (either posts or categories. Will load the filter as an option on one of these Feedwordpress pages ) and a comma-delimited line of required fields.

The static functions are used to define your input controls in the interface. Basically it defines which options you want to give the user.

The only required function is ‘execute’ which is where you put your actual filter functionality and is called when the filter is invoked on a post during the update process.

The display function is used for an upcoming overview page. The use will be to correctly display plugin options there.

Interface

If you want to add a custom field option to your plugin. This is the format of a control. It is fairly straight-forward:

$controls["name of option"] = array(
    "name" => "your field name",
    "desc" => __("you field description"),       
    "type" => "text",
    "default" => "",
    "required" => true);

These controls should be defined in the setControls() function of your filter class

The name of option is used for the $controls property in your class. Name is the field name and will be the name of your HTML field and stored in database. This is also the value you query when executing your filter. Desc will be shown to user.

Type is the type of your control. Currently it can be either ‘text’ ( input type text ) or ‘checkbox’. Others are not yet supported but will be if need arises. The third option can be a custom name like ‘my_field_box’. This allows you to control custom output for a field in your filter. FAF will search for a function by that name in your plugin class ( e.g you need a function my_field_box(); which outputs desired content ).

Default is the default value when the user adds your filter. For a checkbox to be checked the value should be ‘1’, for text a default string of characters.

When required is set the plugin will not save settings but instead give an error stating this options is required. Obviously this is not smart to do on anything of type checkbox since it would for the user to check it.

Executing the filter

The Filter constructor will load two properties in your class: $post and $args. $post is the post_array containing all the fields of the post about to be inserted. $args are all options set, i.e your form fields in an array.

$post = $this->post; 
$args = $this->args; 
$content = $post["post_content"];
$field = $args["your_field_name"]; 

With this information you can filter the post. Important! Your filter should ALWAYS return the updated post array back. If not the whole post will be dropped, only do this if that is the point of your filter.


$post["content"] = $my_updated_content;
return $post;

Don’t forget to update the post array and then return it.

Activation

Drop your filter in the ‘/filters/’ subdirectory of the plugin. Everything in this directory with the extention .php will be loaded into FAF. Be sure to test first locally though since bad coding or fatal errors will break, well, everything. Also be sure that your filter ‘extends’ the fafFilter abstract class!

Samples

Yes this is not for the novice, but it ain’t that hard either. The best thing to do is just look at the already existing filters. You can copy one of them and start editing to fit your needs. As a reminder: this feature is for experienced coders, use at your own peril.

Share!

Feedwordpress Advanced Filters is constantly looking to add new filters to be the best option for Feedwordpress. If you miss a feature and decide to write your own filter please consider sharing it if it would benefit other people as well.

Any questions are welcome in the comments (or support area). I will try to get back to you best way I can.

Tags: , ,


'float:left')); ?>