Create a gist now

Instantly share code, notes, and snippets.

How to adding extend the popular Types Wordpress Plugin (http://wp-types.com/home/types-manage-post-types-taxonomy-and-custom-fields/) by adding new field types without having to work inside the plugin folder.
<?php
/* -- suppose you want to add a new colorpicker field type -- */
add_action( 'init', 'boot_colorpicker' );
/* -- first we create a file in the 'fields' directory of the plugin to notify Types of this new type -- */
function boot_colorpicker(){
$colorpicker_path = WPCF_EMBEDDED_INC_ABSPATH . '/fields/colorpicker.php';
if( !file_exists($colorpicker_path) ){
file_put_contents($colorpicker_path, "<?php // automatically generated \n ?>");
}
}
/* -- then we describe this new field type, giving it a title and a description, note the function name -- */
function wpcf_fields_colorpicker(){
return array(
'id' => 'wpcf-reference',
'title' => "ColorPicker",
'description' => "Add a ColorPicker"
);
}
/* -- then we can use this function (that must be present anyway) to define extra customizable fields for this field type -- */
function wpcf_fields_colorpicker_insert_form($form_data = array(), $parent_name = ''){}
/* -- last we can use this function to describe the behavior of this field type when placed in the edit/create admin post page-- */
function wpcf_fields_colorpicker_meta_box_form($field) {
$element = array(
'#type' => 'markup',
'#markup' => '<b>'.$field['name'].'</b><br/><br/><input type="color" name="wpcf['.$field['slug'].']" id="colorpicker-'.rand().'" value="'.$field['value'].'">'
);
return $element;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment