Pippins Plugins
  • Email
  • Facebook
  • Feedburner
  • Github
  • Google
  • Twitter
  • Vimeo
  • Youtube
  • Rss
  • About
  • News
  • Join the Site
    • Member Benefits
    • Member Plugins
    • Email Notifications
  • Plugin Store
    • Affiliate Area
    • Checkout
  • Plugins
    • Plugin Portfolio
      • Plugin Portfolio – List View
    • Free
    • Premium
    • Member Plugins
    • Coding Standards
    • Get Plugin Support
  • Tutorials
    • Series
      • Plugin Development 101
      • Creating a User Follow System Plugin
      • Customizing Restrict Content Pro
      • Displaying Content with Easy Content Types
      • Writing Your First WordPress Plugins, Basic to Advanced
      • Working with Widgets
      • User Submitted Image Galleries
      • Plugin Thoughts
      • Integrating Stripe.com with WordPress
      • WordPress Rewrite API
    • Member Exclusive
      • Free Members
      • Subscriber Only
    • Difficulty
      • Beginner
      • Intermediate
      • Advanced
    • Action and Filter Hooks
    • Ajax
    • Custom Post Types
    • External APIs
    • Short Codes
    • Taxonomies
    • Video Tutorials
    • Widget Tutorials
    • WordPress Admin / Dashboard
    • Working with jQuery
    • WordPress Database
    • Writing Plugins
    • Tag Index
  • Reviews
  • Support Forum
  • Contact
    • Support the Site
    • Request Code Review
    • Plugin Support

Registering Custom Meta Field Types for Easy Content Types

Posted on January 24, 2012 by Pippin in Action and Filter Hooks, Advanced, Custom Post Types, Taxonomies, Tutorials, Video Tutorials, Writing Plugins 17 Comments
Home» Tutorials » Action and Filter Hooks » Registering Custom Meta Field Types for Easy Content Types
Tweet
Love It - 1

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.

1
2
3
4
5
6
7
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 Types add-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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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.

Download Plugin
Related Items
  • Making a Simple Rating Field Type in Easy Content Types
  • Easy Content Types
Tweet Follow @pippinsplugins
easy content types, meta fields

17 comments on “Registering Custom Meta Field Types for Easy Content Types”

  1. Paul says:
    March 29, 2012 at 9:47 am

    thanks for writing the tutorial!

    Reply
    • Pippin says:
      March 29, 2012 at 9:50 am

      Of course!

  2. designerken says:
    April 17, 2012 at 6:04 pm

    Great tutorial, just what I needed for my custom metabox!

    Reply
  3. willmkt says:
    August 1, 2012 at 4:11 pm

    Hi Pippin,

    I trying to get a metabox to show a list of custom posts, but i don´t know what the problem is.

    case ‘selectPost’ :

    $options = get_posts(array( ‘post_type’ => ‘city’));
    foreach ($options as $option) {
    $selected = ”;
    if($meta == $option) { $selected = ‘selected=”selected”‘; }
    $options .= ”. $option . ”;
    }

    $field_html = ” . $options . ” . __(stripslashes($field['desc']));
    break;

    endswitch;

    Can you tell me what is wrong on the code, or the right way to do it??

    thanks

    Reply
    • Pippin says:
      August 1, 2012 at 8:57 pm

      How do you want to echo the options? As a list or as a drop down?

    • willmkt says:
      August 1, 2012 at 10:43 pm

      Hi Pippin,

      I want to echo as a dropdown, showing all the custom post types

    • Pippin says:
      August 2, 2012 at 9:54 pm

      This should work for you:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      
      case ‘selectPost’ :
       
      	$post_items = get_posts(array( ‘post_type’ => ‘city’));
      	$options = '<select name="' . $field['id'] . '">';
      		foreach ($post_items as $item) {
      			$selected = '';
      			if($meta == $item->ID) { $selected = 'selected="selected"'; }
      			$options .= '<option value="' . $item->ID . '"> . get_the_title( $item->ID ) . '</option>';
      		}
      	$options .= '</select>';
      	$field_html = $options . ' ' . __(stripslashes($field['desc']));
      	break;
       
      endswitch;
  4. Rfourg says:
    November 6, 2012 at 3:08 pm

    Hi Pippin,
    is possible add two or more inputs fields to one meta field type?

    Reply
    • Pippin says:
      November 6, 2012 at 6:29 pm

      Yes, definitely. The data from the fields would be stored as an array. Are you familiar with how to set that up?

  5. Rfourg says:
    November 6, 2012 at 6:41 pm

    I have something like that but I know that it’s incorrectly written. Could you please correct this?

    case 'slide':
    $field_html = '';
    if (is_array($meta)) {
    $count = 1;
    foreach ($meta as $key => $value) {
    //print_r($value);
    $field_html .= ''.
    ''.
    ''.
    'Upload File'.
    'Slide link URL:';

    if ($count > 1) {
    $field_html .= 'x';
    }
    $field_html .= '';
    $count++;
    }
    } else {
    $field_html .= ''.
    ''.
    ''.
    ''.
    'Slide link URL:'.
    ''.
    '';
    }
    $field_html .= '' . __('Add New', 'ecpt') . '  ' . __(stripslashes($field['desc']));
    break;

    Reply
    • Rfourg says:
      November 6, 2012 at 6:52 pm

      The code has been added wrong, so in this case I will try to explain. I would like to add two repeatable input fields to one field type. The first as a upload input and the second as a classic text input field to the url link. Please paste the sample code, if there is no problem.

    • Pippin says:
      November 7, 2012 at 10:08 am

      Can you paste your code into snippi.com and then share the link? That will let me look at what you have so far.

  6. bren says:
    December 11, 2012 at 11:51 pm

    Hi Pippin,

    I’d like to be able to add the following as a single meta entry or at least keep the GUI nice and clean.
    Shipping Data meta comprising of “weight”, “length”, “depth”, “width” and present it on one line.

    So instead of

    “Shipping Data”
    Weight [input field]
    Length [input field]
    Depth [input field]
    Width [input field]

    I’d like to be able to go

    “Shipping Data” : “Wt” [input field] “L x D x W” [input field] [input field] [input field]

    First thing I need to grasp is whether meta data can be stored as an array within an array and if that can be done within your framework of ECPT.
    Or is there an easier way to do this with CSS?

    thanks

    Reply
    • bren says:
      December 11, 2012 at 11:55 pm

      For what it’s worth I have been hacking the bejeezas out of a certain ecommerce plugin and I’m trying to bring my wordpress skills upto the point where I no longer need to do that, i.e. try to do as much as possible with hooks/filters, with the help of your fine tutorials.
      Failing that I need to know at which stage and how to ask the developers for hooks.

    • Pippin says:
      December 12, 2012 at 7:47 pm

      You can definitely store the info as an array. All you need to do is use HTML array notation for the field names, such as name=”meta_name[field]”

      Make sense?

  7. Jens Farvig Thomsen says:
    March 14, 2013 at 4:25 pm

    Hi Pippin,
    First of all – thanks for great plugins, content and tutorials! It has all helped me a lot.

    I was wandering. I have made a custom meta box with a multiple select box that lists all posts from a defined custom post type – see paste bin: http://pastebin.com/WHGNx9Su
    You can then choose some posts and save their ID’s in an array.

    Would it be possible with ECT to make this kinda box and to choose the posts types you want in the select box?

    Fx if I make a CPT of ‘Slides’ then create a box that lists all Slides.
    Make another CPT – fx ‘Spots’, and create another meta box listing all the Spots.

    I hope this make sense, or else I’ll try and elaborate.

    Thanks :-)

    Reply
    • Pippin says:
      March 14, 2013 at 8:33 pm

      It’s definitely possible! If you follow the guide on this post it will show you how :)

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Login

Lost your password?

Please enter your username or e-mail address. You will receive a new password via e-mail.

  • Facebook Become a Fan Like

  • Twitter Subscribe on Twitter Follow

  • YouTube Follow my Videos Subscribe

  • RSS Feed Subscribe with RSS Subscribe

Easy Digital Downloads

Most Loved

  • Love It Pro for WordPress
  • Write a “Love It” Plugin with Ajax to Let Users Love Their Favorite Posts / Pages
  • Simple Notices Pro Plugin for WordPress
  • User Bookmarks for WordPress
  • Front End Registration and Login Forms Plugin

Similar Plugins and Posts

  • Jobs Posting with Easy Content Types and Gravity Forms
  • Review: Easy User Fields
  • Making a Simple Rating Field Type in Easy Content Types
  • Adding a Simple Image Gallery with the Repeatable Upload Field
  • Repeatable Upload Fields for Easy Content Types

Latest Premium Content

  • Plugin Development 101 – Introduction to Adding Dashboard Menus
  • Plugin Development 101 – Intro to Loading Scripts and Styles
  • User Follow System – Part 5
  • Plugin Development 101 – Intro to Short Codes

Latest Tutorials

  • Storing Session Data in WordPress without $_SESSION (19)

    The term Session in web development refers to...

  • Test Your Plugins with RTL (1)

    Right-To-Left languages are those that...

  • Submitting Your First Pull Request to a WordPress Plugin on Github (5)

    Github is an extremely popular tool for managing WordPress plugins, and one...

Enter your email to receive automated updates when new posts are published

WP Core Contributions

  • [24316]

View the ticket on Trac.

WP Codex Contributions

  • Function: shortcode exists
  • Function: has shortcode
  • Function: shortcode exists
  • Function: shortcode exists
  • Function: has shortcode

View all 41 changes in the Codex.

Latest Tweets

  • Could not fetch Twitter RSS feed.

Topics

the_content featured register_setting wp_enqueue_script attachments shortcodes contextual help add_options_page Tom McFarlin get_user_meta hook Sugar Event Calendar meta box forms do_action plugin mail chimp login short codes authors Related posts attachment image apply_filters comments recent posts post types short code bbpress taxonomies custom post type Ajax gallery images Stripe taxonomy jquery widgets users add_filter easy content types add_action widget restrict content pro easy digital downloads

Weekly Newsletter

Useful Links

  • Join the Site
  • Plugin Store
  • Affiliate Area
  • Tag Index
  • Support the Site
  • Suggest a Tutorial
  • Random Post
  • Contact

Monthly Archives

(c) 2013 Pippin's Plugins