Registering Custom Meta Field Types for Easy Content Types

Easy Content Types is a very powerful plugin that makes it really easy to setup custom post types, taxonomies, and meta boxes. One of great but lesser known features of the plugin is that it has an API built in to allow users to register their own custom meta field types that can be used in the custom meta boxes. This tutorial is going to walk you through the process of registering your custom meta field types for Easy Content Types.

The main purpose of being able to easily register new field types is to have the ability to modify Easy Content Type’s behavior, without changing any of the core plugin files. This allows users to make their modifications, and still retain the ability to update the plugin to latest versions. All of the code I’m going to show you will be placed into a custom plugin, which you can download a the bottom of this page.

The entire process is explained in depth in the video, so I will only go into minimal detail here.

Adding new meta field types is quite simple. It requires a minimum of two functions, but no more than three. In order to register our new field types with ECPT, we will use the add_filter() hook. If you’re not familiar with filters, then please read my introductory post on them.

The first function we will write is the function that makes our new field types available to Easy Content Types.

   function sample_ecpt_field_types($field_types) {
	$field_types[] = 'textblock';
	$field_types[] = 'textinput';
 
	return $field_types;
}
add_filter('ecpt_field_types', 'sample_ecpt_field_types');	

Assuming your plugin is active at the time of adding this function, you will now have two new field types available: “textblock” and “textinput”. This function works by taking an array of the existing field types,  $field_types, and then adding two new array elements to it. The returned array is then passed through the “ecpt_field_types” filter, which makes the new types available.

Our field types are now available and you can create new fields with these types through The Easy Content Types interface. They won’t do anything yet, but they will be there.

Now we need to write the function that will output the field HTML inside the meta box.

   function sample_ecpt_fields_html($field_html, $field, $meta) {
	switch($field['type']) :
 
		case 'textblock' :
			$field_html = $field['desc'];
		break;
 
		case 'textinput' :
			$field_html = '<input type="text" name="' . $field['id'] . '" id="' . $field['id'] . '" value="' . $meta . '" class="regular-text"/>';
		break;
 
	endswitch;
 
	return $field_html;
}
add_filter('ecpt_fields_html', 'sample_ecpt_fields_html', 10, 3);	

This function has three parameters:

  1. $field_html – a string containing the HTML markup for the field
  2. $field – an array containing all information about the field (ID, type, description, etc)
  3. $meta – the value stored in the post meta table

Since we need to setup the HTML for each new field type, we perform a switch() statement on the $field[‘type’] array key. For the “textblock” field, we simply return the description of the field. For the “textinput” field, we setup a regular input field. This “textinput” will function exactly like the “text” field that comes with Easy Content Types, but it provides an excellent example.

Once we have setup each of our field type’s HTML, we pass the function through the “ecpt_fields_html” filter. This connects the HTML to the field types.

Both of your fields will function 100% now. You can add them to a meta box, and you can store information in the “textinput” field type. The “textblock” field type works really well for adding contextual information to your meta boxes. These are, of course, simply field type samples, but they should give you an idea of what is possible.

The way that we have just registered these fields is identical to the method I used in my ECPT Bonus Meta Field Typesa dd-on plugin.

There’s just one more thing that we can do with our new field types, if we want. For some field, you may want to change the format of the field label. For example, in the “header” field that is available in ECPT Bonus Meta Field Types, the field label is placed in H4 tags, making it bolder. We can do this with these fields as well.

function sample_ecpt_field_labels($label, $field) {
 
	switch($field['type']) :
 
		case 'textblock' :
			$label = '<h4>' . $label . '</h4>';
		break;
 
		case 'textinput' :
			$label = '';
		break;
 
	endswitch;
 
	return $label;	
 
}
add_filter('ecpt_field_label_filter', 'sample_ecpt_field_labels', 10, 2);	

This function works almost exactly the same as the one we used to setup the fields’ HTML, but only takes two parameters. The “textblock” label is placed in H$ tags, just like my “header” field type. The “textinput” field, however, has its label completely removed by setting $label to a blank string.

That’s it! Your field types are completely finished. In less than 60 lines of code, you have registered new field types for Easy Content Types.

Note, these field types will not be available in the ECPT meta box export function.