WordPress.org

Ready to get started?Download WordPress

Forums

Custom Content Type Manager
Adding a new Custom Field Type (2 posts)

  1. titaniumbones
    Member
    Posted 1 year ago #

    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://wordpress.org/extend/plugins/custom-content-type-manager/

  2. fireproofsocks
    Member
    Plugin Author

    Posted 1 year ago #

    Yeah, I need to spend more time writing up the tutorials here in the docs.

    1. You do not need to include the CCTM or CCTM_FormElement classes: look at the examples inside the fields/ directory: your custom field classes should look exactly the same.

    2. Whenever you're dealing with classes, you need to use REQUIRE_once... moot point, but include_once is technically incorrect.

    3. functions.php is the wrong place to put that. You can create a new class either inside inside your custom directory inside of wp-content/uploads/cctm/fields/. See http://code.google.com/p/wordpress-custom-content-type-manager/wiki/CustomCustomFields

    Or see http://code.google.com/p/wordpress-custom-content-type-manager/wiki/CustomOutputFilters for some examples of how to extend the CCTM with your own classes -- you gotta wrap your head around how YOUR directory overrides and augments the built-in CCTM directories. It's a way for you to override and augment the CCTM functionality from a directory that's safely removed from the one controlled by the WP update process.

    4. Clear the CCTM's cache: CCTM --> Clear cache, and that should force the directories to be rescanned.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic