Easy Custom Post Fields for WordPress
// May 3rd, 2010 // wordpress
Yesterday I added a simple set of classes to the WordPress plugin directory that will allow people with basic PHP development skills to utilize custom post fields for any theme.
Although this script it mainly aimed for developers it can be useful for everyone who is able to define an array in his theme’s functions.php file.
The easiest way to implement it is by downloading the plugin and adding a set of lines like these to your functions.php file
require_once( WP_PLUGIN_DIR . '/easy-custom-fields/easy-custom-fields.php' );
$field_data = array (
'testgroup' => array ( // unique group id
'fields' => array( // array "fields" with field definitions
'field1' => array(), // globally unique field id
'field2' => array(),
'field3' => array(),
),
),
);
$easy_cf = new Easy_CF($field_data);
This is enough to get secure custom fields for your posts. But that’s not all.
More advanced developers can add their own validation rules and field types by extending the existing classes.
require_once( WP_PLUGIN_DIR . '/easy-custom-fields/easy-custom-fields.php' );
$field_data = array (
'testgroup' => array (
'fields' => array(
'field1' => array(),
'field2' => array(),
'field3' => array(),
),
),
'advanced_testgroup' => array ( // unique group id
'fields' => array( // array "fields" with field definitions
'advanced_field' => array( // globally unique field id
'label' => 'Advanced Field Description', // Field Label
'hint' => 'Long Advanced Field description', // A descriptive hint for the field
'type' => 'textarea', // Custom Field Type (see Ref: field_type)
'class' => 'aclass', // CSS Wrapper class for the field
'input_class' => 'theEditor', // CSS class for the input field
'error_msg' => 'The Advanced Field is wrong' ), // Error message to show when validate fails
'validate' => 'validatorname', // Custom Validator (see Ref: validator)
'advanced_email' => array(
'label' => 'Email',
'hint' => 'Enter your email',
'validate' => 'email', )
),
'title' => 'Product Description', // Group Title
'context' => 'advanced', // context as in http://codex.wordpress.org/Function_Reference/add_meta_box
'pages' => array( 'post', 'page' ), // pages as in http://codex.wordpress.org/Function_Reference/add_meta_box
),
);
if ( !class_exists( "Easy_CF_Validator_Email" ) ) {
class Easy_CF_Validator_Email extends Easy_CF_Validator {
public function get( $value='' ) {
return esc_attr( $value );
}
public function set( $value='' ) {
$value = esc_attr( trim( stripslashes( $value ) ) );
return $value;
}
public function validate( $value='' ) {
if ( empty( $value ) || is_email( $value ) )
return true;
else
return false;
}
}
}
if ( !class_exists( "Easy_CF_Field_Textarea" ) ) {
class Easy_CF_Field_Textarea extends Easy_CF_Field {
public function print_form() {
$class = ( empty( $this->_field_data['class'] ) ) ? $this->_field_data['id'] . '_class' : $this->_field_data['class'];
$input_class = ( empty( $this->_field_data['input_class'] ) ) ? $this->_field_data['id'] . '_input_class' : $this->_field_data['input_class'];
$id = ( empty( $this->_field_data['id'] ) ) ? $this->_field_data['id'] : $this->_field_data['id'];
$label = ( empty( $this->_field_data['label'] ) ) ? $this->_field_data['id'] : $this->_field_data['label'];
$value = $this->get();
$hint = ( empty( $this->_field_data['hint'] ) ) ? '' : '<p><em>' . $this->_field_data['hint'] . '</em></p>';
$label_format =
'<div class="%s">'.
'<p><label for="%s"><strong>%s</strong></label></p>'.
'<p><textarea class="%s" style="width: 100%%;" type="text" name="%s">%s</textarea></p>'.
'%s'.
'</div>';
printf( $label_format, $class, $id, $label, $input_class, $id, $value, $hint );
}
}
}
$easy_cf = new Easy_CF($field_data);
Would produce a custom field block with a TinyMCE enhanced textarea along with a field that’s validated to be a valid email address.

Complex fields added with simple code
Please check it out and give me your feedback at : http://wordpress.org/extend/plugins/easy-custom-fields/





I think it’s overkill having a whole class system, it’s not really that easy. Why not have a single function able to register the most common tasks?
You don’t need to use it except if you want to add your own bells and whistles. In the simpliest way you just define the array and initialize the class, that’s it.
One thing I was thinking about though, was to make it also possible to extend the functionality via filters in the common “WordPress” way.