Adding a new Custom Field Type
-
Hi Everett!
I’m trying to implement a new custom field type (eventually this will be part of a project to integrate popcornmaker into WordPress — cf. http;//popcorn.webmaker.org). For now I have just copied the simplest case I could find — the contents of fields/text.php — into the functions.php of my theme. I’ve them made trivial find/replace substitions, but my new field type doesn’t show up in the list at admin.php?page=cctm_fields&a=list_custom_field_types
Any hints? Thanks as always for my favorite plugin! Here’s the code from functions.php:
include_once ( CCTM_PATH . '/includes/CCTM.php'); include_once ( CCTM_PATH . '/includes/CCTM_FormElement.php'); class CCTM_popcorn extends CCTM_FormElement { public $props = array( 'label' => '', 'name' => '', 'description' => '', 'class' => '', 'extra' => '', 'default_value' => '', 'is_repeatable' => '', 'output_filter' => '', 'required' => '', // 'type' => '', // auto-populated: the name of the class, minus the CCTM_ prefix. ); //------------------------------------------------------------------------------ /** * This function provides a name for this type of field. This should return plain * popcorn (no HTML). The returned value should be localized using the __() function. * * @return string */ public function get_name() { return __('Popcorn', CCTM_TXTDOMAIN); } //------------------------------------------------------------------------------ /** * This function gives a description of this type of field so users will know * whether or not they want to add this type of field to their custom content * type. The returned value should be localized using the __() function. * * @return string popcorn description */ public function get_description() { return __('Popcorn fields implement the standard <input="popcorn"> element. "Extra" parameters, e.g. "size" can be specified in the definition.', CCTM_TXTDOMAIN); } //------------------------------------------------------------------------------ /** * This function should return the URL where users can read more information about * the type of field that they want to add to their post_type. The string may * be localized using __() if necessary (e.g. for language-specific pages) * * @return string e.g. http://www.yoursite.com/some/page.html */ public function get_url() { return 'http://code.google.com/p/wordpress-custom-content-type-manager/wiki/Popcorn'; } //------------------------------------------------------------------------------ /** * This is somewhat tricky if the values the user wants to store are HTML/JS. * See http://www.php.net/manual/en/function.htmlspecialchars.php#99185 * * @param mixed $current_value current value for this field. * @return string */ public function get_edit_field_instance($current_value) { // Populate the values (i.e. properties) of this field $this->id = $this->name; $fieldtpl = ''; $wrappertpl = ''; if ($this->is_repeatable) { $fieldtpl = CCTM::load_tpl( array('fields/elements/'.$this->name.'.tpl' , 'fields/elements/_'.$this->type.'_multi.tpl' , 'fields/elements/_default.tpl' ) ); $wrappertpl = CCTM::load_tpl( array('fields/wrappers/'.$this->name.'.tpl' , 'fields/wrappers/_'.$this->type.'_multi.tpl' , 'fields/wrappers/_default.tpl' ) ); $this->i = 0; $values = $this->get_value($current_value,'to_array'); //die(print_r($values,true)); $content = ''; foreach ($values as $v) { $this->value = htmlspecialchars( html_entity_decode($v) ); $this->content .= CCTM::parse($fieldtpl, $this->get_props()); $this->i = $this->i + 1; } } // Normal text field else { $this->value = htmlspecialchars( html_entity_decode($this->get_value($current_value,'to_string') )); $fieldtpl = CCTM::load_tpl( array('fields/elements/'.$this->name.'.tpl' , 'fields/elements/_'.$this->type.'.tpl' , 'fields/elements/_default.tpl' ) ); $wrappertpl = CCTM::load_tpl( array('fields/wrappers/'.$this->name.'.tpl' , 'fields/wrappers/_'.$this->type.'.tpl' , 'fields/wrappers/_default.tpl' ) ); $this->content = CCTM::parse($fieldtpl, $this->get_props()); } $this->add_label = __('Add', CCTM_TXTDOMAIN); return CCTM::parse($wrappertpl, $this->get_props()); } //------------------------------------------------------------------------------ /** * * * @param mixed $def field definition; see the $props array * @return unknown */ public function get_edit_field_definition($def) { // Standard $out = $this->format_standard_fields($def); // Validations / Required $out .= $this->format_validators($def); // Output Filter $out .= $this->format_available_output_filters($def); return $out; } } /*EOF*/http://ww.wp.xz.cn/extend/plugins/custom-content-type-manager/
The topic ‘Adding a new Custom Field Type’ is closed to new replies.