My Fields

This page provides the basic code and brief explainations on how to add new field type into Fields. If you only need to access the code, download it here.

To add new field type, you need to create a new class that extends FS_FieldType with five functions:

  // defines the field type key and title
  public function __construct()
  {
  }

  // show additional controls to add new field type's attributes
  function show_options($field)
  {
  }

  // save the field type's attributes
  function save_options($field, $group)
  {
  }

  // show the field in the add/edit post page
  function show_field($post, $box, $field)
  {
  }

  // save the field's data when a post/page is added/update
  function save_field($postID, $field)
  {
  }

and register the field type like this:

function myfields_register_field()
{
  require('my-simple-field.php'); // contains MY_SimpleField()
  register_field_type(new MY_SimpleField());
}
add_action('fs_init', 'myfields_register_field');

A field showed using the field shortcode can have a specific renderer for it's field type. To register a shortcode handler for a field type, create another class that extends FS_ShortCode with two functions:

//defines the type of field
public function __construct()
{
}

// process the shortcode data and return the string to be shown
function run($field, $atts, $content, $code)
{
}

Register the shortcode handler as follow:

function myfields_register_shortcode()
{
  require('my-simple-field-shortcode.php');

  fs_add_shortcode(new MY_ShortCode());
}

add_action('fs_viewer_init', 'myfields_register_shortcode');

Leave a comment